nemo-relay-cli 0.6.0

Coding-agent gateway CLI for NeMo Relay observability.
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
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

mod logging;
mod types;

pub(crate) use types::*;

use std::collections::HashSet;
use std::env;
use std::fs::{self, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::thread;
use std::time::{Duration, Instant};

use axum::http::{HeaderMap, HeaderValue};
use nemo_relay::logging::LoggingConfig;
use nemo_relay::plugin::dynamic::{
    DYNAMIC_PLUGIN_MANIFEST_FILENAME, DynamicPluginManifest, DynamicPluginManifestLoad,
};
use nemo_relay::plugin::{PluginError, merge_plugin_config_documents};
use ring::rand::{SecureRandom, SystemRandom};
use ring::{digest, hmac};
use serde::Deserialize;
use serde_json::{Map, Value};

use crate::error::CliError;
use crate::filesystem::{LockAttempt, try_lock_exclusive, try_lock_shared};
#[cfg(test)]
use crate::plugins::lifecycle::active_dynamic_plugin_components;
use crate::plugins::lifecycle::{
    ActiveDynamicPluginComponent, active_dynamic_plugin_components_for_identity,
    dynamic_plugin_runtime_closure_digest, enforce_required_dynamic_plugin_startup,
};
use crate::plugins::policy::DynamicPluginHostPolicy;
use crate::process::RunOverrides;
use crate::server::GatewayOverrides;

pub(crate) const BOOTSTRAP_FINGERPRINT_ENV: &str = "NEMO_RELAY_BOOTSTRAP_FINGERPRINT";
pub(crate) const PLUGIN_IDLE_TIMEOUT_ENV: &str = "NEMO_RELAY_PLUGIN_IDLE_TIMEOUT_SECS";
pub(crate) const RELAY_PLUGIN_ID: &str = "nemo-relay-plugin@nemo-relay-local";
pub(crate) const RELAY_SOURCE_PLUGIN_ID: &str = "nemo-relay-plugin@nemo-relay";
pub(crate) const DEFAULT_MAX_HOOK_PAYLOAD_BYTES: usize = 20 * 1024 * 1024;
pub(crate) const DEFAULT_MAX_PASSTHROUGH_BODY_BYTES: usize = 100 * 1024 * 1024;
pub(crate) const GATEWAY_URL_ENV: &str = "NEMO_RELAY_GATEWAY_URL";
pub(crate) const TRANSPARENT_RUN_ENV: &str = "NEMO_RELAY_TRANSPARENT_RUN";

// TOML file shape grouped by user intent. Sections map 1:1 onto fields already present on
// `GatewayConfig` / `AgentConfigs`; plugin configuration lives in `plugins.toml`.
#[derive(Debug, Clone, Default, Deserialize)]
struct FileConfig {
    gateway: Option<FileGatewayConfig>,
    upstream: Option<FileUpstreamConfig>,
    agents: Option<FileAgentsConfig>,
    logging: Option<logging::FileLoggingConfig>,
}

#[derive(Debug, Clone, Default, Deserialize)]
struct FileGatewayConfig {
    max_hook_payload_bytes: Option<usize>,
    max_passthrough_body_bytes: Option<usize>,
}

#[derive(Debug, Clone, Default, Deserialize)]
struct FileUpstreamConfig {
    openai_base_url: Option<String>,
    openai_auth_header: Option<String>,
    anthropic_base_url: Option<String>,
    anthropic_auth_header: Option<String>,
}

#[derive(Debug, Clone, Default, Deserialize)]
struct FileAgentsConfig {
    // Keys match the agent's CLI invocation name (`claude`, `codex`, `hermes`) — the
    // word the user types at the shell — not the product name ("Claude Code") or the internal
    // `CodingAgent` enum kebab spelling. Same convention as the bare-agent shortcut in Phase 2.
    claude: Option<FileAgentCommandConfig>,
    codex: Option<FileAgentCommandConfig>,
    hermes: Option<FileAgentCommandConfig>,
}

#[derive(Debug, Clone, Default, Deserialize)]
struct FileAgentCommandConfig {
    command: Option<String>,
    hooks_path: Option<PathBuf>,
}

/// Resolves server-mode configuration from shared config files plus server CLI/environment overrides.
///
/// File discovery and merge behavior live in `load_shared_config`; this function only applies the
/// server-facing command-line layer so launcher-only settings cannot leak into daemon mode.
pub(crate) fn resolve_server_config(args: &GatewayOverrides) -> Result<ResolvedConfig, CliError> {
    let mut resolved = load_shared_config(args.config.as_ref(), args.plugin_config_path.as_ref())?;
    apply_server_overrides(&mut resolved.gateway, args)?;
    enforce_required_dynamic_plugin_startup(args.config.as_ref(), &resolved)?;
    log::info!(
        target: "nemo_relay.configuration",
        event = "configuration_resolved",
        mode = "server",
        dynamic_plugin_count = resolved.dynamic_plugins.len();
        "Runtime configuration resolved"
    );
    Ok(resolved)
}

/// Resolves only operational logging from the normal config discovery scope.
///
/// This intentionally avoids plugin discovery and activation so logging can be initialized before
/// operational command dispatch. Missing `[logging]` configuration resolves to built-in defaults.
pub(crate) fn resolve_logging_config(
    explicit: Option<&Path>,
    user_only: bool,
) -> Result<LoggingConfig, CliError> {
    let explicit = explicit.map(Path::to_path_buf);
    let user_only = user_only || user_config_scope();
    let mut merged = toml::Value::Table(toml::map::Map::new());
    for path in config_paths_scoped(explicit.as_ref(), user_only) {
        let Some(raw) = read_config_file(&path, explicit.is_some(), "configuration")? else {
            continue;
        };
        let parsed = raw
            .parse::<toml::Table>()
            .map(toml::Value::Table)
            .map_err(|error| {
                CliError::Config(format!("invalid TOML in {}: {error}", path.display()))
            })?;
        merge_toml(&mut merged, parsed);
    }

    if merged.get("logging").is_none() {
        return Ok(LoggingConfig::default());
    }
    let document = toml::to_string(&merged).map_err(|error| {
        CliError::Config(format!("failed to resolve logging configuration: {error}"))
    })?;
    LoggingConfig::from_toml_document(&document).map_err(|error| match error {
        nemo_relay::error::FlowError::InvalidArgument(message) => CliError::Config(message),
        other => CliError::Flow(other),
    })
}

/// Resolves the shared plugin MCP gateway from system and user layers only.
pub(crate) fn resolve_persistent_server_config(
    args: &GatewayOverrides,
) -> Result<ResolvedConfig, CliError> {
    if args.config.is_some() || args.plugin_config_path.is_some() || args.ready_file.is_some() {
        return Err(CliError::Config(
            "nemo-relay mcp uses system and user configuration only; use `nemo-relay run` for explicit or project configuration"
                .into(),
        ));
    }
    let mut resolved = load_shared_config_scoped(None, None, true)?;
    apply_server_overrides(&mut resolved.gateway, args)?;
    let active_dynamic_plugins = active_dynamic_plugin_components_for_identity(None, &resolved)?;
    resolved.bootstrap_fingerprint = Some(persistent_bootstrap_fingerprint(
        &resolved,
        &active_dynamic_plugins,
    )?);
    Ok(resolved)
}

/// Parent-computed identity and inputs needed to reverify a managed persistent gateway child.
#[derive(Debug, Clone)]
pub(crate) struct ManagedBootstrapIdentity {
    expected: String,
    persistent_args: GatewayOverrides,
    resolved: ResolvedConfig,
    active_dynamic_plugins: Vec<ActiveDynamicPluginComponent>,
}

impl ManagedBootstrapIdentity {
    pub(crate) fn fingerprint(&self) -> &str {
        &self.expected
    }

    pub(crate) fn verify_current(&self) -> Result<(), CliError> {
        let snapshot_actual =
            persistent_bootstrap_fingerprint(&self.resolved, &self.active_dynamic_plugins)?;
        verify_managed_bootstrap_fingerprint(&self.expected, &snapshot_actual)?;
        let resolved = resolve_persistent_server_config(&self.persistent_args)?;
        let actual = resolved
            .bootstrap_fingerprint
            .expect("persistent gateway resolution sets a bootstrap fingerprint");
        verify_managed_bootstrap_fingerprint(&self.expected, &actual)
    }
}

/// Verifies and retains the parent-computed identity for a managed persistent gateway child.
///
/// Ordinary daemon launches remain stateless: the internal ready-file contract identifies a child
/// spawned by the plugin bootstrap path. The child recomputes identity from the configuration and
/// active lifecycle records it is about to activate before publishing ownership or readiness.
pub(crate) fn managed_bootstrap_identity(
    args: &GatewayOverrides,
    resolved: &ResolvedConfig,
    active_dynamic_plugins: &[ActiveDynamicPluginComponent],
) -> Result<Option<ManagedBootstrapIdentity>, CliError> {
    if args.ready_file.is_none() {
        return Ok(None);
    }
    let Some(expected) = env::var(BOOTSTRAP_FINGERPRINT_ENV)
        .ok()
        .filter(|fingerprint| !fingerprint.is_empty())
    else {
        return Err(CliError::Config(format!(
            "{BOOTSTRAP_FINGERPRINT_ENV} must be set and non-empty when a managed readiness file is requested"
        )));
    };
    let actual = persistent_bootstrap_fingerprint(resolved, active_dynamic_plugins)?;
    verify_managed_bootstrap_fingerprint(&expected, &actual)?;
    let mut persistent_args = args.clone();
    persistent_args.ready_file = None;
    Ok(Some(ManagedBootstrapIdentity {
        expected,
        persistent_args,
        resolved: resolved.clone(),
        active_dynamic_plugins: active_dynamic_plugins.to_vec(),
    }))
}

fn verify_managed_bootstrap_fingerprint(expected: &str, actual: &str) -> Result<(), CliError> {
    if actual == expected {
        return Ok(());
    }
    Err(CliError::Config(
        "persistent gateway identity changed during managed bootstrap; retry so the parent can resolve the current configuration"
            .into(),
    ))
}

fn persistent_bootstrap_fingerprint(
    resolved: &ResolvedConfig,
    active_dynamic_plugins: &[ActiveDynamicPluginComponent],
) -> Result<String, CliError> {
    let dynamic_plugins = active_dynamic_plugins
        .iter()
        .map(dynamic_plugin_bootstrap_identity)
        .collect::<Result<Vec<_>, _>>()?;
    let gateway = &resolved.gateway;
    let idle_timeout_secs = crate::bootstrap::plugin_idle_timeout()
        .map_err(CliError::Config)?
        .as_secs();
    let document = serde_json::json!({
        "bootstrap_protocol": crate::bootstrap::BOOTSTRAP_PROTOCOL_VERSION,
        "relay_version": env!("CARGO_PKG_VERSION"),
        "openai_base_url": gateway.openai_base_url,
        "openai_auth_header": gateway.openai_auth_header,
        "anthropic_base_url": gateway.anthropic_base_url,
        "anthropic_auth_header": gateway.anthropic_auth_header,
        "metadata": gateway.metadata,
        "plugin_config": gateway.plugin_config,
        "max_hook_payload_bytes": gateway.max_hook_payload_bytes,
        "max_passthrough_body_bytes": gateway.max_passthrough_body_bytes,
        "plugin_idle_timeout_secs": idle_timeout_secs,
        "dynamic_plugins": dynamic_plugins,
        "dynamic_plugin_policy": format!("{:?}", resolved.dynamic_plugin_policy),
    });
    let key = load_or_create_bootstrap_hmac_key()?;
    let key = hmac::Key::new(hmac::HMAC_SHA256, &key);
    let mut digest = hmac::Context::with_key(&key);
    digest.update(
        &serde_json::to_vec(&document).expect("persistent gateway fingerprint serializes to JSON"),
    );
    let environment = env::vars_os().filter_map(|(name, _)| name.into_string().ok());
    for name in crate::mcp_environment::forwarded_names(environment, gateway.plugin_config.as_ref())
    {
        if name == PLUGIN_IDLE_TIMEOUT_ENV {
            continue;
        }
        digest.update(&[0]);
        digest.update(name.as_bytes());
        digest.update(&[0]);
        if let Some(value) = env::var_os(&name) {
            digest.update(value.to_string_lossy().as_bytes());
        }
    }
    let tag = digest.sign();
    Ok(format!(
        "hmac-sha256:{}",
        tag.as_ref()
            .iter()
            .map(|byte| format!("{byte:02x}"))
            .collect::<String>()
    ))
}

fn dynamic_plugin_bootstrap_identity(
    plugin: &ActiveDynamicPluginComponent,
) -> Result<Value, CliError> {
    let manifest_identity = match (&plugin.activation_snapshot, plugin.manifest_ref.as_deref()) {
        (Some(snapshot), _) => Some(dynamic_plugin_snapshot_identity(snapshot)?),
        (None, Some(manifest_ref)) => Some(dynamic_plugin_manifest_identity(
            manifest_ref,
            plugin.environment_ref.as_deref(),
        )?),
        (None, None) => None,
    };
    Ok(serde_json::json!({
        "plugin_id": plugin.plugin_id,
        "kind": format!("{:?}", plugin.kind),
        "lifecycle_generation": plugin.lifecycle_generation,
        "manifest": manifest_identity,
        "environment_ref": plugin.environment_ref,
        "config": plugin.config,
    }))
}

fn dynamic_plugin_snapshot_identity(
    snapshot: &crate::plugins::lifecycle::DynamicPluginActivationSnapshot,
) -> Result<Value, CliError> {
    let (manifest, _) = load_bounded_dynamic_plugin_manifest(snapshot.identity_manifest())?;
    let manifest_path = PathBuf::from(snapshot.original_manifest_ref());
    let manifest_digest = bootstrap_file_digest(
        snapshot.identity_manifest(),
        "dynamic plugin manifest snapshot",
    )?;
    let artifact_ref = manifest
        .source
        .as_ref()
        .and_then(|source| source.artifact.as_deref())
        .or(match &manifest.load {
            DynamicPluginManifestLoad::RustDynamic(load) => load.library.as_deref(),
            DynamicPluginManifestLoad::Worker(_) => None,
        });
    let artifact = artifact_ref
        .map(|artifact_ref| {
            let logical_path = resolve_dynamic_plugin_relative_path(&manifest_path, artifact_ref);
            let snapshot_path = snapshot.identity_file(&logical_path).ok_or_else(|| {
                CliError::Config(format!(
                    "dynamic plugin activation snapshot is missing artifact {}",
                    logical_path.display()
                ))
            })?;
            bootstrap_file_digest(snapshot_path, "dynamic plugin artifact snapshot")
                .map(|digest| serde_json::json!({ "path": logical_path, "sha256": digest }))
        })
        .transpose()?;
    let signature = manifest
        .integrity
        .as_ref()
        .and_then(|integrity| integrity.signature.as_deref())
        .map(|signature_ref| {
            let logical_path = resolve_dynamic_plugin_relative_path(&manifest_path, signature_ref);
            let snapshot_path = snapshot.identity_file(&logical_path).ok_or_else(|| {
                CliError::Config(format!(
                    "dynamic plugin activation snapshot is missing signature {}",
                    logical_path.display()
                ))
            })?;
            bootstrap_file_digest(snapshot_path, "dynamic plugin signature snapshot")
                .map(|digest| serde_json::json!({ "path": logical_path, "sha256": digest }))
        })
        .transpose()?;
    Ok(serde_json::json!({
        "path": snapshot.original_manifest_ref(),
        "sha256": manifest_digest,
        "artifact": artifact,
        "signature": signature,
        "runtime_closure_sha256": snapshot.closure_digest(),
    }))
}

fn dynamic_plugin_manifest_identity(
    manifest_ref: &str,
    environment_ref: Option<&str>,
) -> Result<Value, CliError> {
    let (manifest, normalized_ref) = load_bounded_dynamic_plugin_manifest(manifest_ref)?;
    let manifest_path = PathBuf::from(&normalized_ref);
    let manifest_digest = bootstrap_file_digest(&manifest_path, "dynamic plugin manifest")?;
    let artifact_ref = manifest
        .source
        .as_ref()
        .and_then(|source| source.artifact.as_deref())
        .or(match &manifest.load {
            DynamicPluginManifestLoad::RustDynamic(load) => load.library.as_deref(),
            DynamicPluginManifestLoad::Worker(_) => None,
        });
    let artifact = artifact_ref
        .map(|artifact_ref| {
            let path = resolve_dynamic_plugin_relative_path(&manifest_path, artifact_ref);
            bootstrap_file_digest(&path, "dynamic plugin artifact")
                .map(|digest| serde_json::json!({ "path": path, "sha256": digest }))
        })
        .transpose()?;
    let signature = manifest
        .integrity
        .as_ref()
        .and_then(|integrity| integrity.signature.as_deref())
        .map(|signature_ref| {
            let path = resolve_dynamic_plugin_relative_path(&manifest_path, signature_ref);
            bootstrap_file_digest(&path, "dynamic plugin signature")
                .map(|digest| serde_json::json!({ "path": path, "sha256": digest }))
        })
        .transpose()?;
    let closure_digest = dynamic_plugin_runtime_closure_digest(&normalized_ref, environment_ref)?;
    Ok(serde_json::json!({
        "path": normalized_ref,
        "sha256": manifest_digest,
        "artifact": artifact,
        "signature": signature,
        "runtime_closure_sha256": closure_digest,
    }))
}

fn resolve_dynamic_plugin_relative_path(manifest_path: &Path, reference: &str) -> PathBuf {
    let path = PathBuf::from(reference);
    if path.is_absolute() {
        path
    } else {
        manifest_path
            .parent()
            .map(|parent| parent.join(&path))
            .unwrap_or(path)
    }
}

fn bootstrap_file_digest(path: &Path, description: &str) -> Result<String, CliError> {
    let mut context = digest::Context::new(&digest::SHA256);
    crate::filesystem::bounded::stream_bounded_regular_file(path, description, |bytes| {
        context.update(bytes)
    })
    .map_err(CliError::Config)?;
    Ok(context
        .finish()
        .as_ref()
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect())
}

pub(crate) fn load_bounded_dynamic_plugin_manifest(
    path: impl AsRef<Path>,
) -> Result<(DynamicPluginManifest, String), CliError> {
    let (manifest, normalized, _) = load_bounded_dynamic_plugin_manifest_bytes(path)?;
    Ok((manifest, normalized))
}

pub(crate) fn load_bounded_dynamic_plugin_manifest_bytes(
    path: impl AsRef<Path>,
) -> Result<(DynamicPluginManifest, String, Vec<u8>), CliError> {
    let path = path.as_ref();
    let manifest_path = if path.is_dir() {
        path.join(DYNAMIC_PLUGIN_MANIFEST_FILENAME)
    } else {
        path.to_path_buf()
    };
    let normalized = fs::canonicalize(&manifest_path).map_err(|error| {
        CliError::Config(format!(
            "failed to normalize dynamic plugin manifest {}: {error}",
            manifest_path.display()
        ))
    })?;
    let bytes = crate::filesystem::bounded::read_bounded_regular_file(
        &normalized,
        "dynamic plugin manifest",
    )
    .map_err(CliError::Config)?;
    let contents = std::str::from_utf8(&bytes).map_err(|error| {
        CliError::Config(format!(
            "dynamic plugin manifest {} is not UTF-8: {error}",
            normalized.display()
        ))
    })?;
    let manifest = DynamicPluginManifest::parse_toml(contents)
        .map_err(|error| CliError::Config(error.to_string()))?;
    Ok((manifest, normalized.to_string_lossy().into_owned(), bytes))
}

const BOOTSTRAP_HMAC_KEY_BYTES: usize = 32;
const BOOTSTRAP_HMAC_LOCK_TIMEOUT: Duration = Duration::from_secs(5);
const BOOTSTRAP_CHALLENGE_DOMAIN: &[u8] = b"nemo-relay/bootstrap-health/v1\0";
const BOOTSTRAP_CLIENT_TOKEN_DOMAIN: &[u8] = b"nemo-relay/bootstrap-client/v1\0";
const TRANSPARENT_GATEWAY_DOMAIN: &[u8] = b"nemo-relay/transparent-gateway/v1\0";
const PYTHON_ENVIRONMENT_ATTESTATION_DOMAIN: &[u8] =
    b"nemo-relay/python-environment-attestation/v1\0";

/// Private proof installed into supported coding-agent provider configuration.
pub(crate) const BOOTSTRAP_CLIENT_TOKEN_HEADER: &str = "x-nemo-relay-client-token";

/// Stable health-proof context shared by a transparent wrapper and plugin-owned MCP client.
pub(crate) fn transparent_gateway_fingerprint(gateway_url: &str) -> String {
    let mut context = digest::Context::new(&digest::SHA256);
    context.update(TRANSPARENT_GATEWAY_DOMAIN);
    context.update(gateway_url.as_bytes());
    let encoded = context
        .finish()
        .as_ref()
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect::<String>();
    format!("transparent-sha256:{encoded}")
}

/// Per-user secret used to authenticate a managed bootstrap listener without exposing key bytes.
#[derive(Clone)]
pub(crate) struct BootstrapChallengeKey(hmac::Key);

impl BootstrapChallengeKey {
    pub(crate) fn load() -> Result<Self, CliError> {
        Ok(Self(hmac::Key::new(
            hmac::HMAC_SHA256,
            &load_or_create_bootstrap_hmac_key()?,
        )))
    }

    /// Loads an existing key without creating bootstrap state. Read-only diagnostics use this so
    /// checking an uninstalled integration cannot mutate the user's configuration directory.
    pub(crate) fn load_existing() -> Result<Option<Self>, CliError> {
        load_existing_bootstrap_hmac_key()
            .map(|key| key.map(|key| Self(hmac::Key::new(hmac::HMAC_SHA256, &key))))
    }

    pub(crate) fn proof(&self, fingerprint: &str, nonce: &str) -> String {
        let mut context = hmac::Context::with_key(&self.0);
        context.update(BOOTSTRAP_CHALLENGE_DOMAIN);
        context.update(fingerprint.as_bytes());
        context.update(&[0]);
        context.update(nonce.as_bytes());
        encode_hmac_tag(context.sign())
    }

    pub(crate) fn verify(&self, fingerprint: &str, nonce: &str, proof: &str) -> bool {
        let Some(encoded) = proof.strip_prefix("hmac-sha256:") else {
            return false;
        };
        let Some(tag) = decode_fixed_hex::<32>(encoded) else {
            return false;
        };
        let mut message = Vec::with_capacity(
            BOOTSTRAP_CHALLENGE_DOMAIN.len() + fingerprint.len() + nonce.len() + 1,
        );
        message.extend_from_slice(BOOTSTRAP_CHALLENGE_DOMAIN);
        message.extend_from_slice(fingerprint.as_bytes());
        message.push(0);
        message.extend_from_slice(nonce.as_bytes());
        hmac::verify(&self.0, &message, &tag).is_ok()
    }

    /// Returns a stable, per-user proof that authorizes use of credentials forwarded to a
    /// managed sidecar. The HMAC key remains in Relay's private bootstrap state; coding-agent
    /// configuration stores only this domain-separated proof.
    pub(crate) fn client_token(&self) -> String {
        encode_hmac_tag(hmac::sign(&self.0, BOOTSTRAP_CLIENT_TOKEN_DOMAIN))
    }

    pub(crate) fn verify_client_token(&self, token: &str) -> bool {
        let Some(encoded) = token.strip_prefix("hmac-sha256:") else {
            return false;
        };
        let Some(tag) = decode_fixed_hex::<32>(encoded) else {
            return false;
        };
        hmac::verify(&self.0, BOOTSTRAP_CLIENT_TOKEN_DOMAIN, &tag).is_ok()
    }

    #[cfg(test)]
    pub(crate) fn from_bytes(bytes: &[u8]) -> Self {
        Self(hmac::Key::new(hmac::HMAC_SHA256, bytes))
    }
}

fn encode_hmac_tag(tag: hmac::Tag) -> String {
    format!(
        "hmac-sha256:{}",
        tag.as_ref()
            .iter()
            .map(|byte| format!("{byte:02x}"))
            .collect::<String>()
    )
}

fn decode_fixed_hex<const N: usize>(encoded: &str) -> Option<[u8; N]> {
    if encoded.len() != N * 2 || !encoded.bytes().all(|byte| byte.is_ascii_hexdigit()) {
        return None;
    }
    let mut decoded = [0_u8; N];
    for (index, byte) in decoded.iter_mut().enumerate() {
        *byte = u8::from_str_radix(&encoded[index * 2..index * 2 + 2], 16).ok()?;
    }
    Some(decoded)
}

pub(crate) fn sign_python_environment_attestation(
    source_artifact_sha256: &str,
    environment_sha256: &str,
) -> Result<String, CliError> {
    let key = hmac::Key::new(hmac::HMAC_SHA256, &load_or_create_bootstrap_hmac_key()?);
    let message =
        python_environment_attestation_message(source_artifact_sha256, environment_sha256);
    let tag = hmac::sign(&key, &message);
    Ok(format!(
        "hmac-sha256:{}",
        tag.as_ref()
            .iter()
            .map(|byte| format!("{byte:02x}"))
            .collect::<String>()
    ))
}

pub(crate) fn verify_python_environment_attestation(
    source_artifact_sha256: &str,
    environment_sha256: &str,
    authentication: &str,
) -> Result<bool, CliError> {
    let Some(encoded) = authentication.strip_prefix("hmac-sha256:") else {
        return Ok(false);
    };
    let Some(tag) = decode_fixed_hex::<32>(encoded) else {
        return Ok(false);
    };
    let key = hmac::Key::new(hmac::HMAC_SHA256, &load_or_create_bootstrap_hmac_key()?);
    Ok(hmac::verify(
        &key,
        &python_environment_attestation_message(source_artifact_sha256, environment_sha256),
        &tag,
    )
    .is_ok())
}

fn python_environment_attestation_message(
    source_artifact_sha256: &str,
    environment_sha256: &str,
) -> Vec<u8> {
    let mut message = Vec::with_capacity(
        PYTHON_ENVIRONMENT_ATTESTATION_DOMAIN.len()
            + source_artifact_sha256.len()
            + environment_sha256.len()
            + 1,
    );
    message.extend_from_slice(PYTHON_ENVIRONMENT_ATTESTATION_DOMAIN);
    message.extend_from_slice(source_artifact_sha256.trim().as_bytes());
    message.push(0);
    message.extend_from_slice(environment_sha256.as_bytes());
    message
}

fn load_or_create_bootstrap_hmac_key() -> Result<[u8; BOOTSTRAP_HMAC_KEY_BYTES], CliError> {
    load_or_create_bootstrap_hmac_key_at(&bootstrap_hmac_key_path()?)
}

fn bootstrap_hmac_key_path() -> Result<PathBuf, CliError> {
    user_config_dir()
        .map(|directory| directory.join("bootstrap").join("fingerprint-hmac.key"))
        .ok_or_else(|| {
            CliError::Config(
                "cannot determine the per-user NeMo Relay bootstrap state directory; set HOME or USERPROFILE"
                    .into(),
            )
        })
}

fn load_existing_bootstrap_hmac_key() -> Result<Option<[u8; BOOTSTRAP_HMAC_KEY_BYTES]>, CliError> {
    let path = bootstrap_hmac_key_path()?;
    let mut file = match OpenOptions::new().read(true).open(&path) {
        Ok(file) => file,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            return Err(CliError::Config(format!(
                "failed to open bootstrap HMAC key {}: {error}",
                path.display()
            )));
        }
    };
    let deadline = Instant::now() + BOOTSTRAP_HMAC_LOCK_TIMEOUT;
    loop {
        match try_lock_shared(&file) {
            Ok(LockAttempt::Acquired) => break,
            Ok(LockAttempt::Contended) if Instant::now() < deadline => {
                thread::sleep(Duration::from_millis(25));
            }
            Ok(LockAttempt::Contended) => {
                return Err(CliError::Config(format!(
                    "timed out waiting for bootstrap HMAC key lock {}",
                    path.display()
                )));
            }
            Err(error) => {
                return Err(CliError::Config(format!(
                    "failed to lock bootstrap HMAC key {}: {error}",
                    path.display()
                )));
            }
        }
    }
    let length = file
        .metadata()
        .map_err(|error| {
            CliError::Config(format!(
                "failed to inspect bootstrap HMAC key {}: {error}",
                path.display()
            ))
        })?
        .len();
    if length != BOOTSTRAP_HMAC_KEY_BYTES as u64 {
        return Err(CliError::Config(format!(
            "bootstrap HMAC key {} has invalid length {length}; expected {BOOTSTRAP_HMAC_KEY_BYTES} bytes",
            path.display()
        )));
    }
    let mut key = [0_u8; BOOTSTRAP_HMAC_KEY_BYTES];
    file.read_exact(&mut key).map_err(|error| {
        CliError::Config(format!(
            "failed to read bootstrap HMAC key {}: {error}",
            path.display()
        ))
    })?;
    Ok(Some(key))
}

fn load_or_create_bootstrap_hmac_key_at(
    path: &Path,
) -> Result<[u8; BOOTSTRAP_HMAC_KEY_BYTES], CliError> {
    load_or_create_bootstrap_hmac_key_at_with_timeout(path, BOOTSTRAP_HMAC_LOCK_TIMEOUT)
}

fn load_or_create_bootstrap_hmac_key_at_with_timeout(
    path: &Path,
    lock_timeout: Duration,
) -> Result<[u8; BOOTSTRAP_HMAC_KEY_BYTES], CliError> {
    let parent = path.parent().ok_or_else(|| {
        CliError::Config(format!(
            "bootstrap HMAC key path {} has no parent directory",
            path.display()
        ))
    })?;
    fs::create_dir_all(parent).map_err(|error| {
        CliError::Config(format!(
            "failed to create bootstrap state directory {}: {error}",
            parent.display()
        ))
    })?;
    #[cfg(windows)]
    crate::filesystem::protect_private_windows_path(parent).map_err(|error| {
        CliError::Config(format!(
            "failed to protect bootstrap state directory {}: {error}",
            parent.display()
        ))
    })?;
    #[cfg(unix)]
    fs::set_permissions(parent, {
        use std::os::unix::fs::PermissionsExt;
        fs::Permissions::from_mode(0o700)
    })
    .map_err(|error| {
        CliError::Config(format!(
            "failed to protect bootstrap state directory {}: {error}",
            parent.display()
        ))
    })?;

    #[cfg(windows)]
    let mut file = crate::filesystem::open_private_windows_file(path).map_err(|error| {
        CliError::Config(format!(
            "failed to open bootstrap HMAC key {}: {error}",
            path.display()
        ))
    })?;
    #[cfg(not(windows))]
    let mut file = {
        let mut options = OpenOptions::new();
        options.create(true).truncate(false).read(true).write(true);
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            options.mode(0o600);
        }
        options.open(path).map_err(|error| {
            CliError::Config(format!(
                "failed to open bootstrap HMAC key {}: {error}",
                path.display()
            ))
        })?
    };
    let lock_deadline = Instant::now() + lock_timeout;
    loop {
        match try_lock_exclusive(&file) {
            Ok(LockAttempt::Acquired) => break,
            Ok(LockAttempt::Contended) => {
                if Instant::now() >= lock_deadline {
                    return Err(CliError::Config(format!(
                        "timed out waiting for bootstrap HMAC key lock {}",
                        path.display()
                    )));
                }
                thread::sleep(Duration::from_millis(25));
            }
            Err(error) => {
                return Err(CliError::Config(format!(
                    "failed to lock bootstrap HMAC key {}: {error}",
                    path.display()
                )));
            }
        }
    }
    #[cfg(unix)]
    file.set_permissions({
        use std::os::unix::fs::PermissionsExt;
        fs::Permissions::from_mode(0o600)
    })
    .map_err(|error| {
        CliError::Config(format!(
            "failed to protect bootstrap HMAC key {}: {error}",
            path.display()
        ))
    })?;

    let length = file
        .metadata()
        .map_err(|error| {
            CliError::Config(format!(
                "failed to inspect bootstrap HMAC key {}: {error}",
                path.display()
            ))
        })?
        .len();
    if length == 0 {
        let mut key = [0_u8; BOOTSTRAP_HMAC_KEY_BYTES];
        SystemRandom::new()
            .fill(&mut key)
            .map_err(|_| CliError::Config("failed to generate bootstrap HMAC key".into()))?;
        file.write_all(&key).map_err(|error| {
            CliError::Config(format!(
                "failed to write bootstrap HMAC key {}: {error}",
                path.display()
            ))
        })?;
        file.sync_all().map_err(|error| {
            CliError::Config(format!(
                "failed to persist bootstrap HMAC key {}: {error}",
                path.display()
            ))
        })?;
        return Ok(key);
    }
    if length != BOOTSTRAP_HMAC_KEY_BYTES as u64 {
        return Err(CliError::Config(format!(
            "bootstrap HMAC key {} has invalid length {length}; expected {BOOTSTRAP_HMAC_KEY_BYTES} bytes",
            path.display()
        )));
    }
    file.seek(SeekFrom::Start(0)).map_err(|error| {
        CliError::Config(format!(
            "failed to read bootstrap HMAC key {}: {error}",
            path.display()
        ))
    })?;
    let mut key = [0_u8; BOOTSTRAP_HMAC_KEY_BYTES];
    file.read_exact(&mut key).map_err(|error| {
        CliError::Config(format!(
            "failed to read bootstrap HMAC key {}: {error}",
            path.display()
        ))
    })?;
    Ok(key)
}

/// Resolves shared config for plugin-facing CLI commands without mutating gateway runtime fields.
pub(crate) fn resolve_plugins_config(
    explicit: Option<&PathBuf>,
) -> Result<ResolvedConfig, CliError> {
    let resolved = load_shared_config(explicit, None)?;
    log::info!(
        target: "nemo_relay.configuration",
        event = "plugin_configuration_resolved",
        dynamic_plugin_count = resolved.dynamic_plugins.len();
        "Plugin configuration resolved"
    );
    Ok(resolved)
}

/// Resolves transparent `run` configuration and switches the gateway to an ephemeral bind address.
///
/// Explicit run arguments override inherited top-level server flags, which override shared config.
/// Session metadata and plugin config are parsed as JSON here so malformed CLI values fail before
/// the child agent is spawned.
pub(crate) fn resolve_run_config(
    command: &RunOverrides,
    inherited: Option<&GatewayOverrides>,
) -> Result<ResolvedConfig, CliError> {
    let config = command
        .config
        .as_ref()
        .or_else(|| inherited.and_then(|args| args.config.as_ref()));
    let plugin_config_path = command
        .plugin_config_path
        .as_ref()
        .or_else(|| inherited.and_then(|args| args.plugin_config_path.as_ref()));
    let mut resolved = load_shared_config(config, plugin_config_path)?;
    if let Some(args) = inherited {
        apply_server_overrides(&mut resolved.gateway, args)?;
    }
    apply_run_overrides(&mut resolved.gateway, command)?;
    resolved.gateway.bind = "127.0.0.1:0"
        .parse()
        .expect("valid transparent bind address");
    if !command.dry_run {
        enforce_required_dynamic_plugin_startup(config, &resolved)?;
    }
    log::info!(
        target: "nemo_relay.configuration",
        event = "configuration_resolved",
        mode = "run",
        dynamic_plugin_count = resolved.dynamic_plugins.len();
        "Runtime configuration resolved"
    );
    Ok(resolved)
}

// Applies subcommand-specific `run` overrides after inherited top-level flags. JSON-bearing fields
// are parsed here so invalid metadata or plugin config fails before the gateway binds a port.
fn apply_run_overrides(config: &mut GatewayConfig, command: &RunOverrides) -> Result<(), CliError> {
    apply_run_url_overrides(config, command);
    apply_run_json_overrides(config, command)?;
    Ok(())
}

// Applies plain string/path run overrides. These fields do not need parsing, so they stay separate
// from JSON options whose errors should include field context.
fn apply_run_url_overrides(config: &mut GatewayConfig, command: &RunOverrides) {
    if let Some(value) = &command.openai_base_url {
        config.openai_base_url = value.clone();
    }
    if let Some(value) = &command.anthropic_base_url {
        config.anthropic_base_url = value.clone();
    }
}

// Parses JSON-bearing run overrides after simple values. Invalid metadata or plugin config fails
// before transparent run mode binds its ephemeral gateway listener.
fn apply_run_json_overrides(
    config: &mut GatewayConfig,
    command: &RunOverrides,
) -> Result<(), CliError> {
    if let Some(value) = &command.session_metadata {
        config.metadata = Some(parse_json_option("session metadata", value)?);
    }
    Ok(())
}

// Applies direct server flags on top of already-merged configuration. Only present options mutate
// the config so lower-priority file values survive when a flag was omitted.
fn apply_server_overrides(
    config: &mut GatewayConfig,
    args: &GatewayOverrides,
) -> Result<(), CliError> {
    if let Some(value) = args.bind {
        config.bind = value;
    }
    if let Some(value) = &args.openai_base_url {
        config.openai_base_url = value.clone();
    }
    if let Some(value) = &args.anthropic_base_url {
        config.anthropic_base_url = value.clone();
    }
    if let Some(value) = args.max_hook_payload_bytes {
        config.max_hook_payload_bytes = validate_body_limit("max hook payload bytes", value)?;
    }
    if let Some(value) = args.max_passthrough_body_bytes {
        config.max_passthrough_body_bytes =
            validate_body_limit("max passthrough body bytes", value)?;
    }
    Ok(())
}

pub(crate) const PLUGINS_TOML: &str = "plugins.toml";

// Loads config from the ordered shared locations, deep-merges TOML tables, maps the typed file
// shape onto runtime structs, applies a sibling/discovered plugins.toml when present, then lets
// environment variables override file values. Invalid TOML or typed shapes fail closed because
// they indicate an operator configuration error.
fn load_shared_config(
    explicit: Option<&PathBuf>,
    plugin_config_path: Option<&PathBuf>,
) -> Result<ResolvedConfig, CliError> {
    load_shared_config_scoped(explicit, plugin_config_path, user_config_scope())
}

fn load_shared_config_scoped(
    explicit: Option<&PathBuf>,
    plugin_config_path: Option<&PathBuf>,
    user_only: bool,
) -> Result<ResolvedConfig, CliError> {
    let mut merged = toml::Value::Table(toml::map::Map::new());
    for path in config_paths_scoped(explicit, user_only) {
        let Some(raw) = read_config_file(&path, explicit.is_some(), "configuration")? else {
            continue;
        };
        let parsed = raw
            .parse::<toml::Table>()
            .map(toml::Value::Table)
            .map_err(|error| {
                CliError::Config(format!("invalid TOML in {}: {error}", path.display()))
            })?;
        let legacy_observability = legacy_observability_sections(&parsed);
        if !legacy_observability.is_empty() {
            return Err(CliError::Config(format!(
                "legacy observability config in {} is no longer supported: {}; configure \
                 observability in plugins.toml with `nemo-relay plugins edit`",
                path.display(),
                legacy_observability.join(", ")
            )));
        }
        if parsed.get("plugins").is_some() {
            return Err(CliError::Config(format!(
                "plugin configuration in {} is no longer supported; move it to plugins.toml",
                path.display()
            )));
        }
        merge_gateway_config_toml(&mut merged, parsed);
    }
    let plugin_toml = load_plugin_toml_config_scoped(explicit, plugin_config_path, user_only)?;
    let mut resolved = ResolvedConfig {
        gateway: GatewayConfig::default(),
        ..ResolvedConfig::default()
    };
    apply_file_config(&mut resolved, merged)?;
    apply_plugin_toml_config(&mut resolved, plugin_toml);
    apply_env_config(&mut resolved.gateway)?;
    Ok(resolved)
}

fn read_config_file(
    path: &Path,
    required: bool,
    description: &str,
) -> Result<Option<String>, CliError> {
    match path.try_exists() {
        Ok(false) if !required => Ok(None),
        Ok(false) => Err(CliError::Config(format!(
            "explicit {description} file {} does not exist",
            path.display()
        ))),
        Err(error) => Err(CliError::Config(format!(
            "failed to inspect {description} file {}: {error}",
            path.display()
        ))),
        Ok(true) => std::fs::read_to_string(path).map(Some).map_err(|error| {
            CliError::Config(format!(
                "failed to read {description} file {}: {error}",
                path.display()
            ))
        }),
    }
}

/// Returns true if any of the implicit config file locations exists on disk. Used by the
/// easy-path dispatcher to decide whether to launch setup (no config found) or proceed
/// with config-driven settings. Mirrors `config_paths(None)` but only checks existence.
pub(crate) fn any_config_file_exists() -> bool {
    config_paths(None).iter().any(|path| path.exists())
}

// Returns the config search path. An explicit path disables implicit discovery; otherwise system
// config is lowest priority, the nearest project config is next, and user config is merged last.
fn config_paths(explicit: Option<&PathBuf>) -> Vec<PathBuf> {
    config_paths_scoped(explicit, user_config_scope())
}

fn config_paths_scoped(explicit: Option<&PathBuf>, user_only: bool) -> Vec<PathBuf> {
    if let Some(path) = explicit {
        return vec![path.clone()];
    }
    let mut paths = vec![PathBuf::from("/etc/nemo-relay/config.toml")];
    if !user_only
        && let Ok(cwd) = std::env::current_dir()
        && let Some(project) = find_project_config(&cwd)
    {
        paths.push(project);
    }
    if let Some(user) = user_config_path() {
        paths.push(user);
    }
    paths
}

// Returns the plugin config search path. An explicit gateway config path scopes plugins.toml to the
// same directory so `--config path/to/config.toml` can be extended by `path/to/plugins.toml` without
// reading unrelated implicit project/user/global plugin files.
fn plugin_config_paths(
    explicit: Option<&PathBuf>,
    plugin_config_path: Option<&PathBuf>,
) -> Vec<PathBuf> {
    plugin_config_paths_scoped(explicit, plugin_config_path, user_config_scope())
}

fn plugin_config_paths_scoped(
    explicit: Option<&PathBuf>,
    plugin_config_path: Option<&PathBuf>,
    user_only: bool,
) -> Vec<PathBuf> {
    if let Some(path) = plugin_config_path {
        return vec![path.clone()];
    }
    if let Some(path) = explicit {
        return path
            .parent()
            .map(|parent| vec![parent.join(PLUGINS_TOML)])
            .unwrap_or_default();
    }
    if user_only {
        return implicit_plugin_config_paths(None, user_config_dir());
    }
    implicit_plugin_config_paths(std::env::current_dir().ok().as_deref(), user_config_dir())
}

fn user_config_scope() -> bool {
    std::env::var("NEMO_RELAY_CONFIG_SCOPE").ok().as_deref() == Some("user")
}

/// Returns the implicit `plugins.toml` discovery paths used by the gateway and doctor.
pub(crate) fn default_plugin_config_paths() -> Vec<PathBuf> {
    plugin_config_paths(None, None)
}

fn implicit_plugin_config_paths(
    cwd: Option<&std::path::Path>,
    user_config_dir: Option<PathBuf>,
) -> Vec<PathBuf> {
    // The search-path logic lives in core; the gateway shares it so discovery stays identical.
    nemo_relay::plugin::default_plugin_config_paths(cwd, user_config_dir)
}

// Walks upward from the current directory and returns the nearest project-local gateway config.
// The first hit wins so nested projects can override parent workspace defaults.
fn find_project_config(start: &std::path::Path) -> Option<PathBuf> {
    for ancestor in start.ancestors() {
        let path = ancestor.join(".nemo-relay/config.toml");
        if path.exists() {
            return Some(path);
        }
    }
    None
}

// The project-walk lives in core; the gateway shares it so discovery stays identical.
fn find_project_plugin_config(start: &std::path::Path) -> Option<PathBuf> {
    nemo_relay::plugin::nearest_project_plugin_config(start)
}

pub(crate) fn user_plugin_config_path() -> Option<PathBuf> {
    user_config_dir().map(|dir| dir.join(PLUGINS_TOML))
}

pub(crate) fn user_plugin_runtime_config() -> Result<Option<Value>, CliError> {
    Ok(
        load_plugin_toml_config_from_paths(implicit_plugin_config_paths(None, user_config_dir()))?
            .and_then(|config| config.value),
    )
}

pub(crate) fn project_plugin_config_path(start: &std::path::Path) -> PathBuf {
    find_project_plugin_config(start)
        .or_else(|| {
            find_project_config(start)
                .and_then(|path| path.parent().map(|parent| parent.join(PLUGINS_TOML)))
        })
        .unwrap_or_else(|| start.join(".nemo-relay").join(PLUGINS_TOML))
}

pub(crate) fn global_plugin_config_path() -> PathBuf {
    PathBuf::from("/etc/nemo-relay").join(PLUGINS_TOML)
}

// Resolves the user config using XDG first and HOME/USERPROFILE second. Returning `None` keeps
// config loading portable in minimal environments where no home directory is visible.
fn user_config_path() -> Option<PathBuf> {
    user_config_dir().map(|dir| dir.join("config.toml"))
}

/// Resolves the nemo-relay user config DIRECTORY (without trailing filename). Delegates to core's
/// resolver so the gateway, the editor, and the plugin runtime agree on the location.
pub(crate) fn user_config_dir() -> Option<PathBuf> {
    nemo_relay::plugin::user_config_dir()
}

// Applies the typed TOML config model to the resolved runtime config. Missing sections and fields
// are ignored, preserving defaults and prior merge layers.
fn apply_file_config(resolved: &mut ResolvedConfig, value: toml::Value) -> Result<(), CliError> {
    let config: FileConfig = value.try_into().map_err(|error| {
        CliError::Config(format!("invalid gateway configuration shape: {error}"))
    })?;
    apply_file_gateway_config(&mut resolved.gateway, config.gateway)?;
    apply_file_upstream_config(&mut resolved.gateway, config.upstream)?;
    apply_file_agents_config(&mut resolved.agents, config.agents);
    logging::apply_file_logging_config(&mut resolved.logging, config.logging)?;
    Ok(())
}

fn apply_file_gateway_config(
    gateway: &mut GatewayConfig,
    config: Option<FileGatewayConfig>,
) -> Result<(), CliError> {
    let Some(config) = config else {
        return Ok(());
    };
    if let Some(value) = config.max_hook_payload_bytes {
        gateway.max_hook_payload_bytes =
            validate_body_limit("gateway.max_hook_payload_bytes", value)?;
    }
    if let Some(value) = config.max_passthrough_body_bytes {
        gateway.max_passthrough_body_bytes =
            validate_body_limit("gateway.max_passthrough_body_bytes", value)?;
    }
    Ok(())
}

// Applies upstream LLM provider URLs. These are the bases for OpenAI- and Anthropic-shaped
// gateway routes; transparent `run` mode can still override them per invocation.
fn apply_file_upstream_config(
    gateway: &mut GatewayConfig,
    upstream: Option<FileUpstreamConfig>,
) -> Result<(), CliError> {
    let Some(upstream) = upstream else {
        return Ok(());
    };
    let FileUpstreamConfig {
        openai_base_url,
        openai_auth_header,
        anthropic_base_url,
        anthropic_auth_header,
    } = upstream;
    if let Some(value) = openai_base_url {
        gateway.openai_base_url = value;
        if openai_auth_header.is_none() {
            gateway.openai_auth_header = None;
        }
    }
    if let Some(value) = openai_auth_header {
        gateway.openai_auth_header =
            Some(validate_auth_header("upstream.openai_auth_header", value)?);
    }
    if let Some(value) = anthropic_base_url {
        gateway.anthropic_base_url = value;
        if anthropic_auth_header.is_none() {
            gateway.anthropic_auth_header = None;
        }
    }
    if let Some(value) = anthropic_auth_header {
        gateway.anthropic_auth_header = Some(validate_auth_header(
            "upstream.anthropic_auth_header",
            value,
        )?);
    }
    Ok(())
}

#[derive(Debug, Clone)]
struct PluginTomlConfig {
    value: Option<Value>,
    dynamic_plugins: Vec<ResolvedDynamicPluginConfig>,
    dynamic_plugin_policy: DynamicPluginHostPolicy,
    contributing_sources: Vec<PathBuf>,
}

#[derive(Debug, Clone, Default, Deserialize)]
struct PluginTomlPluginsSection {
    #[serde(default)]
    dynamic: Vec<FileDynamicPluginConfig>,
    #[serde(default)]
    policy: Option<crate::plugins::policy::FileDynamicPluginHostPolicy>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
struct FileDynamicPluginConfig {
    manifest: String,
    #[serde(default)]
    config: Option<Map<String, Value>>,
}

fn load_plugin_toml_config(
    explicit: Option<&PathBuf>,
    plugin_config_path: Option<&PathBuf>,
) -> Result<Option<PluginTomlConfig>, CliError> {
    load_plugin_toml_config_scoped(explicit, plugin_config_path, user_config_scope())
}

fn load_plugin_toml_config_scoped(
    explicit: Option<&PathBuf>,
    plugin_config_path: Option<&PathBuf>,
    user_only: bool,
) -> Result<Option<PluginTomlConfig>, CliError> {
    load_plugin_toml_config_from_paths(plugin_config_paths_scoped(
        explicit,
        plugin_config_path,
        user_only,
    ))
}

/// Returns the physical `plugins.toml` files that contribute effective runtime or dynamic
/// plugin configuration under the default discovery rules.
pub(crate) fn effective_plugin_toml_sources() -> Result<Vec<PathBuf>, CliError> {
    let Some(config) = load_plugin_toml_config(None, None)? else {
        return Ok(Vec::new());
    };
    let mut sources = config.contributing_sources;
    sources.sort();
    sources.dedup();
    Ok(sources)
}

fn load_plugin_toml_config_from_paths<I>(paths: I) -> Result<Option<PluginTomlConfig>, CliError>
where
    I: IntoIterator<Item = PathBuf>,
{
    let paths = paths.into_iter().collect::<Vec<_>>();
    let mut dynamic_plugins = Vec::new();
    let mut dynamic_plugin_policy = DynamicPluginHostPolicy::default();
    let mut seen_plugin_ids = HashSet::new();
    let mut contributing_sources = Vec::new();
    let mut runtime_documents = Vec::new();

    for path in &paths {
        let Some(raw) = read_config_file(path, false, "plugin configuration")? else {
            continue;
        };
        let mut parsed = raw
            .parse::<toml::Table>()
            .map(toml::Value::Table)
            .map_err(|error| {
                CliError::Config(format!(
                    "invalid plugin TOML in {}: {error}",
                    path.display()
                ))
            })?;
        let resolved_plugins =
            resolve_dynamic_plugin_refs(path, &mut parsed, &mut seen_plugin_ids)?;
        if !resolved_plugins.dynamic_plugins.is_empty()
            || resolved_plugins.dynamic_plugin_policy != DynamicPluginHostPolicy::default()
        {
            contributing_sources.push(path.clone());
        }
        dynamic_plugins.extend(resolved_plugins.dynamic_plugins);
        dynamic_plugin_policy.merge_from(resolved_plugins.dynamic_plugin_policy);
        runtime_documents.push((
            path.clone(),
            serde_json::to_value(remove_dynamic_plugin_sections(parsed))
                .expect("toml value serializes to JSON"),
        ));
    }

    // Delegate merged runtime plugin config to the shared core primitive after dynamic refs have
    // been validated independently. File precedence stays unchanged for the generic runtime path.
    let resolved = merge_plugin_config_documents(runtime_documents).map_err(|err| match err {
        PluginError::InvalidConfig(message) => CliError::Config(message),
        other => CliError::Config(other.to_string()),
    })?;
    match resolved {
        Some((value, sources)) => {
            contributing_sources.extend(sources.iter().cloned());
            contributing_sources.sort();
            contributing_sources.dedup();
            Ok(Some(PluginTomlConfig {
                value: plugin_toml_runtime_value(value),
                dynamic_plugins,
                dynamic_plugin_policy,
                contributing_sources,
            }))
        }
        None => Ok((!dynamic_plugins.is_empty()
            || dynamic_plugin_policy != DynamicPluginHostPolicy::default())
        .then_some(PluginTomlConfig {
            value: None,
            dynamic_plugins,
            dynamic_plugin_policy,
            contributing_sources,
        })),
    }
}

fn apply_plugin_toml_config(resolved: &mut ResolvedConfig, plugin_toml: Option<PluginTomlConfig>) {
    let Some(plugin_toml) = plugin_toml else {
        return;
    };
    if let Some(value) = plugin_toml.value {
        resolved.gateway.plugin_config = Some(value);
    }
    resolved.dynamic_plugins = plugin_toml.dynamic_plugins;
    resolved.dynamic_plugin_policy = plugin_toml.dynamic_plugin_policy;
}

struct ResolvedDynamicPluginRefs {
    dynamic_plugins: Vec<ResolvedDynamicPluginConfig>,
    dynamic_plugin_policy: DynamicPluginHostPolicy,
}

fn resolve_dynamic_plugin_refs(
    source: &Path,
    value: &mut toml::Value,
    seen_plugin_ids: &mut HashSet<String>,
) -> Result<ResolvedDynamicPluginRefs, CliError> {
    let Some(root) = value.as_table_mut() else {
        return Ok(ResolvedDynamicPluginRefs {
            dynamic_plugins: Vec::new(),
            dynamic_plugin_policy: DynamicPluginHostPolicy::default(),
        });
    };

    let plugins_value = root.get("plugins").cloned();
    let Some(plugins_value) = plugins_value else {
        return Ok(ResolvedDynamicPluginRefs {
            dynamic_plugins: Vec::new(),
            dynamic_plugin_policy: DynamicPluginHostPolicy::default(),
        });
    };

    let plugins: PluginTomlPluginsSection = plugins_value.try_into().map_err(|error| {
        CliError::Config(format!(
            "invalid dynamic plugin config in {}: {error}",
            source.display()
        ))
    })?;

    if let Some(toml::Value::Table(plugins_table)) = root.get_mut("plugins") {
        plugins_table.remove("dynamic");
        plugins_table.remove("policy");
        if plugins_table.is_empty() {
            root.remove("plugins");
        }
    }

    let mut resolved = Vec::with_capacity(plugins.dynamic.len());
    for dynamic in plugins.dynamic {
        let manifest_path = resolve_dynamic_manifest_path(source, &dynamic.manifest);
        let (manifest, manifest_ref) = load_bounded_dynamic_plugin_manifest(&manifest_path)
            .map_err(|error| {
                CliError::Config(format!(
                    "invalid dynamic plugin manifest referenced by {}: {error}",
                    source.display()
                ))
            })?;
        let plugin_id = manifest.plugin.id.trim().to_owned();
        if !seen_plugin_ids.insert(plugin_id.clone()) {
            return Err(CliError::Config(format!(
                "duplicate dynamic plugin id '{}' in {} across plugins.toml sources",
                plugin_id,
                source.display()
            )));
        }
        resolved.push(ResolvedDynamicPluginConfig {
            plugin_id,
            manifest_ref,
            has_explicit_config: dynamic.config.is_some(),
            config: dynamic.config.unwrap_or_default(),
            source: source.to_path_buf(),
        });
    }
    Ok(ResolvedDynamicPluginRefs {
        dynamic_plugins: resolved,
        dynamic_plugin_policy: plugins.policy.map(Into::into).unwrap_or_default(),
    })
}

fn resolve_dynamic_manifest_path(source: &Path, manifest: &str) -> PathBuf {
    let manifest = PathBuf::from(manifest);
    if manifest.is_absolute() {
        manifest
    } else {
        source
            .parent()
            .map(|parent| parent.join(&manifest))
            .unwrap_or(manifest)
    }
}

fn plugin_toml_runtime_value(value: Value) -> Option<Value> {
    match value {
        Value::Object(ref object) if object.is_empty() => None,
        other => Some(other),
    }
}

fn remove_dynamic_plugin_sections(mut value: toml::Value) -> toml::Value {
    if let Some(root) = value.as_table_mut()
        && let Some(toml::Value::Table(plugins)) = root.get_mut("plugins")
    {
        plugins.remove("dynamic");
        plugins.remove("policy");
        if plugins.is_empty() {
            root.remove("plugins");
        }
    }
    value
}

// Applies configured agent commands from the merged file configuration.
fn apply_file_agents_config(agents: &mut AgentConfigs, file_agents: Option<FileAgentsConfig>) {
    let Some(file_agents) = file_agents else {
        return;
    };
    if let Some(value) = file_agents.claude {
        agents.claude.command = value.command;
    }
    if let Some(value) = file_agents.codex {
        agents.codex.command = value.command;
    }
    if let Some(value) = file_agents.hermes {
        agents.hermes.command = value.command;
        agents.hermes.hooks_path = value.hooks_path;
    }
}

// Applies environment variables after file configuration. Invalid bind values are ignored here to
// preserve existing startup behavior, while string values replace earlier layers when present.
fn apply_env_config(config: &mut GatewayConfig) -> Result<(), CliError> {
    if let Ok(value) = std::env::var("NEMO_RELAY_GATEWAY_BIND")
        && let Ok(value) = value.parse()
    {
        config.bind = value;
    }
    let openai_auth_header = std::env::var("NEMO_RELAY_OPENAI_AUTH_HEADER").ok();
    if let Ok(value) = std::env::var("NEMO_RELAY_OPENAI_BASE_URL") {
        config.openai_base_url = value;
        if openai_auth_header.is_none() {
            config.openai_auth_header = None;
        }
    }
    if let Some(value) = openai_auth_header {
        config.openai_auth_header = Some(validate_auth_header(
            "NEMO_RELAY_OPENAI_AUTH_HEADER",
            value,
        )?);
    }
    let anthropic_auth_header = std::env::var("NEMO_RELAY_ANTHROPIC_AUTH_HEADER").ok();
    if let Ok(value) = std::env::var("NEMO_RELAY_ANTHROPIC_BASE_URL") {
        config.anthropic_base_url = value;
        if anthropic_auth_header.is_none() {
            config.anthropic_auth_header = None;
        }
    }
    if let Some(value) = anthropic_auth_header {
        config.anthropic_auth_header = Some(validate_auth_header(
            "NEMO_RELAY_ANTHROPIC_AUTH_HEADER",
            value,
        )?);
    }
    if let Ok(value) = std::env::var("NEMO_RELAY_MAX_HOOK_PAYLOAD_BYTES") {
        config.max_hook_payload_bytes =
            parse_env_body_limit("NEMO_RELAY_MAX_HOOK_PAYLOAD_BYTES", &value)?;
    }
    if let Ok(value) = std::env::var("NEMO_RELAY_MAX_PASSTHROUGH_BODY_BYTES") {
        config.max_passthrough_body_bytes =
            parse_env_body_limit("NEMO_RELAY_MAX_PASSTHROUGH_BODY_BYTES", &value)?;
    }
    Ok(())
}

fn validate_auth_header(name: &str, value: String) -> Result<String, CliError> {
    let value = value.trim().to_string();
    if value.is_empty() {
        return Err(CliError::Config(format!("{name} must not be empty")));
    }
    HeaderValue::from_str(&value)
        .map_err(|_| CliError::Config(format!("{name} must be a valid HTTP header value")))?;
    Ok(value)
}

fn parse_env_body_limit(name: &str, raw: &str) -> Result<usize, CliError> {
    let value = raw.parse::<usize>().map_err(|error| {
        CliError::Config(format!("{name} must be a positive byte count: {error}"))
    })?;
    validate_body_limit(name, value)
}

fn validate_body_limit(name: &str, value: usize) -> Result<usize, CliError> {
    if value == 0 {
        return Err(CliError::Config(format!("{name} must be greater than 0")));
    }
    Ok(value)
}

// Recursively merges TOML tables and replaces scalar/array values from the higher-priority side.
// This lets user/project configs override individual nested keys without restating whole sections.
fn merge_toml(left: &mut toml::Value, right: toml::Value) {
    match (left, right) {
        (toml::Value::Table(left), toml::Value::Table(right)) => {
            for (key, value) in right {
                match left.get_mut(&key) {
                    Some(existing) => merge_toml(existing, value),
                    None => {
                        left.insert(key, value);
                    }
                }
            }
        }
        (left, right) => *left = right,
    }
}

// Upstream credentials are bound to their configured endpoint. A higher-priority layer that
// changes an endpoint without supplying a replacement credential must not inherit the credential
// for the old endpoint.
fn merge_gateway_config_toml(left: &mut toml::Value, right: toml::Value) {
    if let (Some(existing), Some(override_upstream)) = (
        left.get_mut("upstream").and_then(toml::Value::as_table_mut),
        right.get("upstream").and_then(toml::Value::as_table),
    ) {
        for (base_url, auth_header) in [
            ("openai_base_url", "openai_auth_header"),
            ("anthropic_base_url", "anthropic_auth_header"),
        ] {
            let endpoint_changed = override_upstream
                .get(base_url)
                .is_some_and(|value| existing.get(base_url) != Some(value));
            if endpoint_changed && !override_upstream.contains_key(auth_header) {
                existing.remove(auth_header);
            }
        }
    }
    merge_toml(left, right);
}

fn legacy_observability_sections(value: &toml::Value) -> Vec<&'static str> {
    let mut sections = Vec::new();
    if value.get("exporters").is_some() {
        sections.push("[exporters]");
    }
    if value.get("observability").is_some() {
        sections.push("[observability]");
    }
    if value
        .get("export")
        .and_then(|export| export.get("openinference"))
        .is_some()
    {
        sections.push("[export.openinference]");
    }
    sections
}

// Parses JSON-valued CLI options into runtime metadata/config values and labels errors with the
// user-facing option name so callers can report which structured argument was malformed.
fn parse_json_option(name: &str, value: &str) -> Result<Value, CliError> {
    serde_json::from_str::<Value>(value)
        .map_err(|error| CliError::Config(format!("invalid {name}: {error}")))
}

/// Reads a non-empty UTF-8 header value as an owned string.
///
/// Invalid header bytes and empty strings are treated as absent so callers can preserve their
/// explicit fallback order without surfacing HTTP parsing details as gateway errors.
pub(crate) fn header_string(headers: &HeaderMap, name: &str) -> Option<String> {
    headers
        .get(name)
        .and_then(|value| value.to_str().ok())
        .filter(|value| !value.is_empty())
        .map(ToOwned::to_owned)
}

fn header_json(headers: &HeaderMap, name: &str) -> Option<Value> {
    header_string(headers, name).and_then(|raw| serde_json::from_str(&raw).ok())
}

#[cfg(test)]
#[path = "../../tests/coverage/shared/config_tests.rs"]
mod tests;