emelex 1.1.1

Apple Silicon local inference toolkit powered by MLX
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
//! Strict global and project configuration loading.

use std::{
	fmt,
	fs::{self, File, OpenOptions},
	io::{Read as _, Write as _},
	os::{
		fd::AsRawFd as _,
		unix::fs::{MetadataExt as _, OpenOptionsExt as _, PermissionsExt as _},
	},
	path::{Path, PathBuf},
	time::Duration,
};

use serde::{Deserialize, Serialize};

use crate::{
	agent::{MAX_SHELL_OUTPUT_BYTES, MAX_WEB_RESPONSE_BYTES},
	home::EmelexHome,
	hub::HubCredentials,
	model::ModelRef,
};

const HUB_TOKEN_REQUIREMENT: &str = "Hugging Face token must contain 1..=4096 visible ASCII bytes";

macro_rules! apply_copy {
	($source:ident, $target:ident, $($field:ident),+ $(,)?) => {
		$(
			if let Some(value) = $source.$field {
				$target.$field = value;
			}
		)+
	};
}

/// Fully resolved configuration snapshot.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct Config {
	/// Default model reference used when no command override exists.
	pub default_model: Option<ModelRef>,
	/// Generation behavior.
	pub inference: InferenceConfig,
	/// Agent and tool behavior.
	pub agent: AgentConfig,
	/// Hub discovery and transfer bounds.
	pub hub: HubConfig,
	/// Durable memory behavior.
	pub memory: MemoryConfig,
	/// Translation command defaults.
	pub translate: TranslateConfig,
}

/// Default language pair for `emelex translate`. Not authority-bearing:
/// a project `.emelex.toml` may set or clear it freely.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct TranslateConfig {
	/// Default source language code (BCP-47-style, e.g. "en").
	pub source: Option<String>,
	/// Default target language code (BCP-47-style, e.g. "de").
	pub target: Option<String>,
}

/// Generation defaults.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct InferenceConfig {
	/// Maximum generated tokens.
	pub max_tokens: usize,
	/// Workload context assumption used for fit estimation.
	pub context_tokens: usize,
	/// Sampling temperature.
	pub temperature: f32,
	/// Nucleus-sampling threshold.
	pub top_p: f32,
	/// Optional top-k cutoff.
	pub top_k: Option<u32>,
	/// Optional deterministic seed.
	pub seed: Option<u64>,
	/// Thinking-mode policy.
	pub thinking: ThinkingMode,
	/// Enable verified MTP automatically.
	pub mtp: bool,
	/// Draft depth when MTP is enabled.
	pub speculative_tokens: usize,
	/// Reuse prompt KV state.
	pub prompt_cache: bool,
}

impl Default for InferenceConfig {
	fn default() -> Self {
		Self {
			max_tokens: 4096,
			context_tokens: 16_384,
			temperature: 0.0,
			top_p: 1.0,
			top_k: None,
			seed: None,
			thinking: ThinkingMode::Auto,
			mtp: false,
			speculative_tokens: 4,
			prompt_cache: true,
		}
	}
}

/// Thinking-mode selection.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ThinkingMode {
	/// Use the client default; when unset, ask the template to disable reasoning.
	#[default]
	Auto,
	/// Request reasoning.
	On,
	/// Ask the template to disable reasoning.
	Off,
}

/// Agent and built-in tool defaults.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct AgentConfig {
	/// Optional generic system instruction appended after immutable policy.
	pub system_prompt: Option<String>,
	/// Maximum assistant/tool loop turns for non-interactive runs such as
	/// `emelex generate`. Interactive `emelex chat` runs without a ceiling.
	pub max_turns: usize,
	/// Enable HTTP(S) tools.
	pub web: bool,
	/// Enable workspace file tools.
	pub files: bool,
	/// Enable host shell tool behind approval.
	pub shell: bool,
	/// Per-command shell timeout in seconds.
	pub shell_timeout_seconds: u64,
	/// Maximum captured shell output bytes.
	pub shell_output_bytes: usize,
	/// Maximum fetched web response bytes.
	pub web_response_bytes: usize,
}

impl Default for AgentConfig {
	fn default() -> Self {
		Self {
			system_prompt: None,
			max_turns: 20,
			web: true,
			files: true,
			shell: true,
			shell_timeout_seconds: 120,
			shell_output_bytes: MAX_SHELL_OUTPUT_BYTES,
			web_response_bytes: MAX_WEB_RESPONSE_BYTES,
		}
	}
}

impl AgentConfig {
	/// Shell timeout.
	pub const fn shell_timeout(&self) -> Duration {
		Duration::from_secs(self.shell_timeout_seconds)
	}
}

/// Hugging Face discovery and transfer limits.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct HubConfig {
	/// Compatible results requested per page.
	pub results: usize,
	/// Maximum Hub candidates inspected per search call.
	pub scan_limit: usize,
	/// Concurrent metadata preflights.
	pub metadata_concurrency: usize,
	/// HTTP request timeout in seconds.
	pub request_timeout_seconds: u64,
	/// Transfer retry count after the first attempt.
	pub retries: usize,
}

impl Default for HubConfig {
	fn default() -> Self {
		Self {
			results: 20,
			scan_limit: 200,
			metadata_concurrency: 8,
			request_timeout_seconds: 30,
			retries: 3,
		}
	}
}

impl HubConfig {
	/// HTTP timeout.
	pub const fn request_timeout(&self) -> Duration {
		Duration::from_secs(self.request_timeout_seconds)
	}

	pub(crate) fn validate(&self) -> Result<(), ConfigError> {
		if self.results == 0
			|| self.results > 100
			|| self.scan_limit < self.results
			|| self.scan_limit > 1000
			|| self.metadata_concurrency == 0
			|| self.metadata_concurrency > 32
			|| !(1..=300).contains(&self.request_timeout_seconds)
			|| self.retries > 10
		{
			return Err(ConfigError::Invalid(
				"Hub limits require results 1..=100, scan_limit between results \
				 and 1000, metadata_concurrency 1..=32, request timeout 1..=300 \
				 seconds, and retries at most 10"
					.to_string(),
			));
		}
		Ok(())
	}
}

/// Session and Knowledge defaults.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct MemoryConfig {
	/// Inactive-data retention window.
	pub retention_days: u32,
	/// Optional model reference used for compaction/distillation.
	pub model: Option<ModelRef>,
	/// Maximum automatically recalled entries.
	pub recall_entries: usize,
	/// Maximum serialized bytes injected by automatic memory recall.
	pub recall_bytes: usize,
	/// Minimum automatic Knowledge confidence.
	pub confidence_threshold: f32,
}

impl Default for MemoryConfig {
	fn default() -> Self {
		Self {
			retention_days: 30,
			model: None,
			recall_entries: 8,
			recall_bytes: 6 * 1024,
			confidence_threshold: 0.70,
		}
	}
}

/// Configuration files selected for one invocation.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConfigSources {
	/// Global configuration, when present.
	pub global: Option<PathBuf>,
	/// Git-root project configuration, when present.
	pub project: Option<PathBuf>,
}

pub(crate) struct LoadedConfig {
	pub(crate) config: Config,
	pub(crate) sources: ConfigSources,
	pub(crate) hub_credentials: Option<HubCredentials>,
}

/// Strict configuration loading failures.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ConfigError {
	/// Configuration file could not be read.
	#[error("cannot read configuration {path:?}: {source}")]
	Read {
		/// Configuration path.
		path: PathBuf,
		/// Underlying I/O failure.
		#[source]
		source: std::io::Error,
	},
	/// Global configuration could not be replaced durably.
	#[error("cannot write configuration {path:?}: {source}")]
	Write {
		/// Affected configuration or temporary path.
		path: PathBuf,
		/// Underlying I/O failure.
		#[source]
		source: std::io::Error,
	},
	/// Global configuration changed during a read-modify-write update.
	#[error("configuration {path:?} changed concurrently; retry the operation")]
	ConcurrentModification {
		/// Configuration path.
		path: PathBuf,
	},
	/// Configuration syntax or fields are invalid.
	#[error("invalid configuration {path:?}: {message}")]
	Parse {
		/// Configuration path.
		path: PathBuf,
		/// Parser explanation.
		message: String,
	},
	/// A resolved numeric policy is unsafe or nonsensical.
	#[error("invalid resolved configuration: {0}")]
	Invalid(String),
}

impl Config {
	/// Load defaults, global configuration, then optional project
	/// configuration.
	///
	/// Project discovery starts at `invocation_root` and stops at the nearest
	/// ancestor containing `.git` as a file or directory.
	///
	/// # Errors
	///
	/// Unknown keys, malformed TOML, unreadable files, or invalid bounds fail
	/// before model loading.
	pub fn load(
		home: &EmelexHome,
		invocation_root: &Path,
		load_project: bool,
	) -> Result<(Self, ConfigSources), ConfigError> {
		let loaded = Self::load_for_emelex(home, invocation_root, load_project)?;
		Ok((loaded.config, loaded.sources))
	}

	pub(crate) fn load_for_emelex(
		home: &EmelexHome,
		invocation_root: &Path,
		load_project: bool,
	) -> Result<LoadedConfig, ConfigError> {
		let global_path = home.config_file();
		let global = read_optional_patch(&global_path)?;
		let hub_credentials = match global.as_ref() {
			Some(global) => global.global_hub_credentials(&global_path)?,
			None => None,
		};
		let project_path = load_project
			.then(|| project_root(invocation_root).map(|root| root.join(".emelex.toml")))
			.flatten();
		let project = match project_path.as_ref() {
			Some(path) => {
				let patch = read_optional_patch(path)?;
				if let Some(patch) = &patch {
					patch.validate_project(path)?;
				}
				patch
			}
			None => None,
		};
		let mut config = Self::default();
		if let Some(patch) = global {
			patch.apply(&mut config, PatchAuthority::Global);
		}
		if let Some(patch) = project {
			patch.apply(&mut config, PatchAuthority::Project);
		}
		config.validate()?;
		Ok(LoadedConfig {
			config,
			sources: ConfigSources {
				global: global_path.is_file().then_some(global_path),
				project: project_path.filter(|path| path.is_file()),
			},
			hub_credentials,
		})
	}

	/// Atomically set or clear the global default model while preserving every
	/// other global setting.
	///
	/// Project configuration is never read or written by this operation.
	///
	/// # Errors
	///
	/// Returns when the existing global file is unsafe or invalid, the updated
	/// configuration would be invalid, or durable replacement fails.
	pub fn write_global_default_model(
		home: &EmelexHome,
		model: Option<&ModelRef>,
	) -> Result<(), ConfigError> {
		let _lock = ConfigWriteLock::acquire(home)?;
		let path = home.config_file();
		let original = read_optional_text(&path)?;
		let text = original.clone().unwrap_or_default();
		let mut table =
			toml::from_str::<toml::Table>(&text).map_err(|error| ConfigError::Parse {
				path: path.clone(),
				message: error.message().to_string(),
			})?;
		match model {
			Some(model) => {
				table.insert(
					"default_model".to_string(),
					toml::Value::String(model.to_string()),
				);
			}
			None => {
				table.remove("default_model");
			}
		}
		let rendered = toml::to_string_pretty(&table).map_err(|error| ConfigError::Parse {
			path: path.clone(),
			message: error.to_string(),
		})?;
		let patch =
			toml::from_str::<ConfigPatch>(&rendered).map_err(|error| ConfigError::Parse {
				path: path.clone(),
				message: error.message().to_string(),
			})?;
		patch.validate_global_hub_credentials(&path)?;
		let mut resolved = Self::default();
		patch.apply(&mut resolved, PatchAuthority::Global);
		resolved.validate()?;
		write_global_config(home, &path, rendered.as_bytes(), original.as_deref())
	}

	/// Atomically set or clear the global Hugging Face token while preserving
	/// every other global setting.
	///
	/// Project configuration is never read or written by this operation.
	///
	/// # Errors
	///
	/// Returns when the token or existing global file is invalid, the updated
	/// configuration would be invalid, or durable replacement fails.
	pub fn write_global_hub_token(
		home: &EmelexHome,
		token: Option<&str>,
	) -> Result<(), ConfigError> {
		if let Some(token) = token {
			validate_hub_token(token)?;
		}
		let _lock = ConfigWriteLock::acquire(home)?;
		let path = home.config_file();
		let original = read_optional_text(&path)?;
		let text = original.clone().unwrap_or_default();
		let mut table =
			toml::from_str::<toml::Table>(&text).map_err(|error| ConfigError::Parse {
				path: path.clone(),
				message: error.message().to_string(),
			})?;
		let remove_empty_hub = match token {
			Some(token) => {
				let hub = table
					.entry("hub".to_string())
					.or_insert_with(|| toml::Value::Table(toml::Table::new()))
					.as_table_mut()
					.ok_or_else(|| ConfigError::Parse {
						path: path.clone(),
						message: "hub configuration must be a table".to_string(),
					})?;
				hub.insert("token".to_string(), toml::Value::String(token.to_string()));
				false
			}
			None => match table.get_mut("hub") {
				Some(value) => {
					let hub = value.as_table_mut().ok_or_else(|| ConfigError::Parse {
						path: path.clone(),
						message: "hub configuration must be a table".to_string(),
					})?;
					hub.remove("token");
					hub.is_empty()
				}
				None => false,
			},
		};
		if remove_empty_hub {
			table.remove("hub");
		}
		let rendered = toml::to_string_pretty(&table).map_err(|error| ConfigError::Parse {
			path: path.clone(),
			message: error.to_string(),
		})?;
		let patch =
			toml::from_str::<ConfigPatch>(&rendered).map_err(|error| ConfigError::Parse {
				path: path.clone(),
				message: error.message().to_string(),
			})?;
		patch.validate_global_hub_credentials(&path)?;
		let mut resolved = Self::default();
		patch.apply(&mut resolved, PatchAuthority::Global);
		resolved.validate()?;
		write_global_config(home, &path, rendered.as_bytes(), original.as_deref())
	}

	/// Whether the global configuration contains a usable Hugging Face token.
	///
	/// Project configuration is never inspected.
	///
	/// # Errors
	///
	/// Returns when the global configuration is unreadable or invalid.
	pub fn global_hub_token_configured(home: &EmelexHome) -> Result<bool, ConfigError> {
		Ok(Self::load_for_emelex(home, home.root(), false)?
			.hub_credentials
			.is_some())
	}

	/// Validate every resolved limit and cross-field invariant.
	///
	/// # Errors
	///
	/// Returns [`ConfigError::Invalid`] when any field is outside its
	/// supported range or conflicts with another field.
	pub fn validate(&self) -> Result<(), ConfigError> {
		if !(1..=1 << 20).contains(&self.inference.max_tokens)
			|| !(1..=1 << 20).contains(&self.inference.context_tokens)
			|| self.inference.speculative_tokens > 8
		{
			return Err(ConfigError::Invalid(
				"inference token limits must be in 1..=1048576 and \
				 speculative_tokens at most 8"
					.to_string(),
			));
		}
		if self.inference.max_tokens > self.inference.context_tokens {
			return Err(ConfigError::Invalid(
				"inference.max_tokens must not exceed inference.context_tokens".to_string(),
			));
		}
		if !self.inference.temperature.is_finite()
			|| !self.inference.top_p.is_finite()
			|| !(0.0..=2.0).contains(&self.inference.temperature)
			|| !(0.0..=1.0).contains(&self.inference.top_p)
		{
			return Err(ConfigError::Invalid(
				"temperature must be 0..=2 and top_p must be 0..=1".to_string(),
			));
		}
		if !(1..=crate::agent::MAX_AGENT_MODEL_ROUNDS).contains(&self.agent.max_turns)
			|| !(1..=crate::agent::MAX_SHELL_TIMEOUT_SECONDS)
				.contains(&self.agent.shell_timeout_seconds)
			|| !(1..=MAX_SHELL_OUTPUT_BYTES).contains(&self.agent.shell_output_bytes)
			|| !(1..=MAX_WEB_RESPONSE_BYTES).contains(&self.agent.web_response_bytes)
		{
			return Err(ConfigError::Invalid(
				"agent bounds require max_turns 1..=20, shell timeout 1..=1200 \
				 seconds, shell output 1..=524288 bytes, and web response \
				 1..=1048576 bytes"
					.to_string(),
			));
		}
		self.hub.validate()?;
		if !(1..=3650).contains(&self.memory.retention_days)
			|| !(1..=100).contains(&self.memory.recall_entries)
			|| !(1..=1_048_576).contains(&self.memory.recall_bytes)
			|| !self.memory.confidence_threshold.is_finite()
			|| !(0.0..=1.0).contains(&self.memory.confidence_threshold)
		{
			return Err(ConfigError::Invalid(
				"memory bounds require retention_days 1..=3650, recall_entries \
				 1..=100, recall_bytes 1..=1048576, and confidence_threshold \
				 0..=1"
					.to_string(),
			));
		}
		Ok(())
	}
}

#[derive(Clone, Deserialize)]
struct ConfiguredHubToken(String);

impl fmt::Debug for ConfiguredHubToken {
	fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
		formatter.write_str("ConfiguredHubToken([REDACTED])")
	}
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct ConfigPatch {
	default_model: Option<PatchValue<ModelRef>>,
	inference: Option<InferencePatch>,
	agent: Option<AgentPatch>,
	hub: Option<HubPatch>,
	memory: Option<MemoryPatch>,
	translate: Option<TranslatePatch>,
}

impl ConfigPatch {
	fn validate_global_hub_credentials(&self, path: &Path) -> Result<(), ConfigError> {
		self.global_hub_credentials(path).map(|_| ())
	}

	fn global_hub_credentials(&self, path: &Path) -> Result<Option<HubCredentials>, ConfigError> {
		let Some(token) = self.hub.as_ref().and_then(|hub| hub.token.as_ref()) else {
			return Ok(None);
		};
		match token {
			PatchValue::Set(token) => {
				HubCredentials::bearer_token(&token.0)
					.map(Some)
					.map_err(|_| ConfigError::Parse {
						path: path.to_path_buf(),
						message: HUB_TOKEN_REQUIREMENT.to_string(),
					})
			}
			PatchValue::Clear(_) => Ok(None),
		}
	}

	fn validate_project(&self, path: &Path) -> Result<(), ConfigError> {
		let mut forbidden = Vec::new();
		if self.default_model.is_some() {
			forbidden.push("default_model");
		}
		if let Some(inference) = &self.inference {
			if inference.seed.is_some() {
				forbidden.push("inference.seed");
			}
			if matches!(inference.top_k, Some(PatchValue::Clear(_))) {
				forbidden.push("inference.top_k clear");
			}
			if matches!(inference.top_k, Some(PatchValue::Set(0))) {
				forbidden.push("inference.top_k = 0");
			}
			if inference
				.thinking
				.is_some_and(|mode| mode != ThinkingMode::Off)
			{
				forbidden.push("inference.thinking on/auto");
			}
		}
		if self
			.agent
			.as_ref()
			.is_some_and(|agent| agent.system_prompt.is_some())
		{
			forbidden.push("agent.system_prompt");
		}
		if self.hub.as_ref().is_some_and(|hub| hub.token.is_some()) {
			forbidden.push("hub.token");
		}
		if let Some(memory) = &self.memory {
			if memory.model.is_some() {
				forbidden.push("memory.model");
			}
			if memory.retention_days.is_some() {
				forbidden.push("memory.retention_days");
			}
		}
		if forbidden.is_empty() {
			Ok(())
		} else {
			Err(ConfigError::Parse {
				path: path.to_path_buf(),
				message: format!(
					"project configuration cannot set authority-bearing field(s): {}",
					forbidden.join(", ")
				),
			})
		}
	}

	fn apply(self, config: &mut Config, authority: PatchAuthority) {
		if authority == PatchAuthority::Global
			&& let Some(value) = self.default_model
		{
			config.default_model = value.into_option();
		}
		if let Some(patch) = self.inference {
			patch.apply(&mut config.inference, authority);
		}
		if let Some(patch) = self.agent {
			patch.apply(&mut config.agent, authority);
		}
		if let Some(patch) = self.hub {
			patch.apply(&mut config.hub, authority);
		}
		if let Some(patch) = self.memory {
			patch.apply(&mut config.memory, authority);
		}
		if let Some(patch) = self.translate {
			patch.apply(&mut config.translate);
		}
	}
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct TranslatePatch {
	source: Option<PatchValue<String>>,
	target: Option<PatchValue<String>>,
}

impl TranslatePatch {
	/// Language-pair defaults are not authority-bearing; project and
	/// global scope apply identically.
	fn apply(self, target: &mut TranslateConfig) {
		if let Some(value) = self.source {
			target.source = value.into_option();
		}
		if let Some(value) = self.target {
			target.target = value.into_option();
		}
	}
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct InferencePatch {
	max_tokens: Option<usize>,
	context_tokens: Option<usize>,
	temperature: Option<f32>,
	top_p: Option<f32>,
	top_k: Option<PatchValue<u32>>,
	seed: Option<PatchValue<u64>>,
	thinking: Option<ThinkingMode>,
	mtp: Option<bool>,
	speculative_tokens: Option<usize>,
	prompt_cache: Option<bool>,
}

impl InferencePatch {
	fn apply(self, target: &mut InferenceConfig, authority: PatchAuthority) {
		if authority == PatchAuthority::Project {
			if let Some(value) = self.max_tokens {
				target.max_tokens = target.max_tokens.min(value);
			}
			if let Some(value) = self.context_tokens {
				target.context_tokens = target.context_tokens.min(value);
			}
			if let Some(value) = self.temperature {
				target.temperature = target.temperature.min(value);
			}
			if let Some(value) = self.top_p {
				target.top_p = target.top_p.min(value);
			}
			if let Some(PatchValue::Set(value)) = self.top_k {
				target.top_k = Some(target.top_k.map_or(value, |current| current.min(value)));
			}
			if self.thinking == Some(ThinkingMode::Off) {
				target.thinking = ThinkingMode::Off;
			}
			if let Some(value) = self.mtp {
				target.mtp &= value;
			}
			if let Some(value) = self.speculative_tokens {
				target.speculative_tokens = target.speculative_tokens.min(value);
			}
			if let Some(value) = self.prompt_cache {
				target.prompt_cache &= value;
			}
			return;
		}
		apply_copy!(self, target, max_tokens, context_tokens, temperature, top_p);
		if let Some(value) = self.top_k {
			target.top_k = value.into_option();
		}
		if let Some(value) = self.seed {
			target.seed = value.into_option();
		}
		apply_copy!(self, target, thinking, mtp);
		apply_copy!(self, target, speculative_tokens, prompt_cache);
	}
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct AgentPatch {
	system_prompt: Option<PatchValue<String>>,
	max_turns: Option<usize>,
	web: Option<bool>,
	files: Option<bool>,
	shell: Option<bool>,
	shell_timeout_seconds: Option<u64>,
	shell_output_bytes: Option<usize>,
	web_response_bytes: Option<usize>,
}

impl AgentPatch {
	fn apply(self, target: &mut AgentConfig, authority: PatchAuthority) {
		if authority == PatchAuthority::Global
			&& let Some(value) = self.system_prompt
		{
			target.system_prompt = value.into_option();
		}
		if authority == PatchAuthority::Project {
			if let Some(value) = self.web {
				target.web &= value;
			}
			if let Some(value) = self.files {
				target.files &= value;
			}
			if let Some(value) = self.shell {
				target.shell &= value;
			}
			if let Some(value) = self.max_turns {
				target.max_turns = target.max_turns.min(value);
			}
			if let Some(value) = self.shell_timeout_seconds {
				target.shell_timeout_seconds = target.shell_timeout_seconds.min(value);
			}
			if let Some(value) = self.shell_output_bytes {
				target.shell_output_bytes = target.shell_output_bytes.min(value);
			}
			if let Some(value) = self.web_response_bytes {
				target.web_response_bytes = target.web_response_bytes.min(value);
			}
			return;
		}
		apply_copy!(self, target, max_turns, web, files, shell);
		apply_copy!(
			self,
			target,
			shell_timeout_seconds,
			shell_output_bytes,
			web_response_bytes
		);
	}
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct HubPatch {
	results: Option<usize>,
	scan_limit: Option<usize>,
	metadata_concurrency: Option<usize>,
	request_timeout_seconds: Option<u64>,
	retries: Option<usize>,
	token: Option<PatchValue<ConfiguredHubToken>>,
}

impl HubPatch {
	fn apply(self, target: &mut HubConfig, authority: PatchAuthority) {
		if authority == PatchAuthority::Project {
			if let Some(value) = self.results {
				target.results = target.results.min(value);
			}
			if let Some(value) = self.scan_limit {
				target.scan_limit = target.scan_limit.min(value);
			}
			if let Some(value) = self.metadata_concurrency {
				target.metadata_concurrency = target.metadata_concurrency.min(value);
			}
			if let Some(value) = self.request_timeout_seconds {
				target.request_timeout_seconds = target.request_timeout_seconds.min(value);
			}
			if let Some(value) = self.retries {
				target.retries = target.retries.min(value);
			}
			return;
		}
		apply_copy!(
			self,
			target,
			results,
			scan_limit,
			metadata_concurrency,
			request_timeout_seconds,
			retries
		);
	}
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct MemoryPatch {
	retention_days: Option<u32>,
	model: Option<PatchValue<ModelRef>>,
	recall_entries: Option<usize>,
	recall_bytes: Option<usize>,
	confidence_threshold: Option<f32>,
}

impl MemoryPatch {
	fn apply(self, target: &mut MemoryConfig, authority: PatchAuthority) {
		if authority == PatchAuthority::Project {
			if let Some(value) = self.recall_entries {
				target.recall_entries = target.recall_entries.min(value);
			}
			if let Some(value) = self.recall_bytes {
				target.recall_bytes = target.recall_bytes.min(value);
			}
			if let Some(value) = self.confidence_threshold {
				target.confidence_threshold = target.confidence_threshold.max(value);
			}
			return;
		}
		apply_copy!(self, target, retention_days, recall_entries, recall_bytes);
		apply_copy!(self, target, confidence_threshold);
		if let Some(value) = self.model {
			target.model = value.into_option();
		}
	}
}

fn validate_hub_token(token: &str) -> Result<(), ConfigError> {
	HubCredentials::bearer_token(token)
		.map(|_| ())
		.map_err(|_| ConfigError::Invalid(HUB_TOKEN_REQUIREMENT.to_string()))
}

fn read_optional_patch(path: &Path) -> Result<Option<ConfigPatch>, ConfigError> {
	let Some(text) = read_optional_text(path)? else {
		return Ok(None);
	};
	toml::from_str::<ConfigPatch>(&text)
		.map(Some)
		.map_err(|error| ConfigError::Parse {
			path: path.to_path_buf(),
			message: error.message().to_string(),
		})
}

fn read_optional_text(path: &Path) -> Result<Option<String>, ConfigError> {
	const MAX_CONFIG_BYTES: u64 = 1 << 20;
	let mut file = match OpenOptions::new()
		.read(true)
		.custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_NONBLOCK)
		.open(path)
	{
		Ok(file) => file,
		Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
		Err(source) => {
			return Err(ConfigError::Read {
				path: path.to_path_buf(),
				source,
			});
		}
	};
	let metadata = file.metadata().map_err(|source| ConfigError::Read {
		path: path.to_path_buf(),
		source,
	})?;
	if !metadata.is_file() || metadata.len() > MAX_CONFIG_BYTES {
		return Err(ConfigError::Parse {
			path: path.to_path_buf(),
			message: "configuration must be a regular file no larger than 1 MiB".to_string(),
		});
	}
	let capacity = usize::try_from(metadata.len()).map_err(|_| ConfigError::Parse {
		path: path.to_path_buf(),
		message: "configuration size does not fit memory".to_string(),
	})?;
	let mut bytes = Vec::with_capacity(capacity);
	std::io::Read::by_ref(&mut file)
		.take(MAX_CONFIG_BYTES + 1)
		.read_to_end(&mut bytes)
		.map_err(|source| ConfigError::Read {
			path: path.to_path_buf(),
			source,
		})?;
	if bytes.len() as u64 > MAX_CONFIG_BYTES {
		return Err(ConfigError::Parse {
			path: path.to_path_buf(),
			message: "configuration must be no larger than 1 MiB".to_string(),
		});
	}
	let text = String::from_utf8(bytes).map_err(|error| ConfigError::Parse {
		path: path.to_path_buf(),
		message: format!("configuration is not UTF-8: {error}"),
	})?;
	Ok(Some(text))
}

fn write_global_config(
	home: &EmelexHome,
	path: &Path,
	contents: &[u8],
	expected: Option<&str>,
) -> Result<(), ConfigError> {
	let temporary = home.root().join(format!(
		".config.toml.{}.{}.tmp",
		std::process::id(),
		uuid::Uuid::now_v7()
	));
	let result = write_global_config_inner(home, path, &temporary, contents, expected);
	if result.is_err() {
		let _ = fs::remove_file(&temporary);
	}
	result
}

fn write_global_config_inner(
	home: &EmelexHome,
	path: &Path,
	temporary: &Path,
	contents: &[u8],
	expected: Option<&str>,
) -> Result<(), ConfigError> {
	let mut file = OpenOptions::new()
		.create_new(true)
		.write(true)
		.mode(0o600)
		.custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW)
		.open(temporary)
		.map_err(|source| ConfigError::Write {
			path: temporary.to_path_buf(),
			source,
		})?;
	file.write_all(contents)
		.and_then(|()| file.sync_all())
		.map_err(|source| ConfigError::Write {
			path: temporary.to_path_buf(),
			source,
		})?;
	if read_optional_text(path)?.as_deref() != expected {
		return Err(ConfigError::ConcurrentModification {
			path: path.to_path_buf(),
		});
	}
	fs::rename(temporary, path).map_err(|source| ConfigError::Write {
		path: path.to_path_buf(),
		source,
	})?;
	File::open(home.root())
		.and_then(|directory| directory.sync_all())
		.map_err(|source| ConfigError::Write {
			path: home.root().to_path_buf(),
			source,
		})
}

struct ConfigWriteLock(File);

impl ConfigWriteLock {
	fn acquire(home: &EmelexHome) -> Result<Self, ConfigError> {
		let path = home.root().join(".config.lock");
		let file = OpenOptions::new()
			.read(true)
			.write(true)
			.create(true)
			.mode(0o600)
			.custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW)
			.open(&path)
			.map_err(|source| ConfigError::Write {
				path: path.clone(),
				source,
			})?;
		let metadata = file.metadata().map_err(|source| ConfigError::Write {
			path: path.clone(),
			source,
		})?;
		if !metadata.is_file()
			|| metadata.uid() != crate::home::effective_user_id()
			|| metadata.permissions().mode() & 0o777 != 0o600
		{
			return Err(ConfigError::Write {
				path,
				source: std::io::Error::new(
					std::io::ErrorKind::PermissionDenied,
					"configuration lock must be an owner-only regular file",
				),
			});
		}
		loop {
			// SAFETY: file owns a live descriptor and LOCK_EX is a valid operation.
			if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX) } == 0 {
				return Ok(Self(file));
			}
			let source = std::io::Error::last_os_error();
			if source.kind() != std::io::ErrorKind::Interrupted {
				return Err(ConfigError::Write { path, source });
			}
		}
	}
}

impl Drop for ConfigWriteLock {
	fn drop(&mut self) {
		// SAFETY: the descriptor remains live until this guard finishes dropping.
		unsafe {
			libc::flock(self.0.as_raw_fd(), libc::LOCK_UN);
		}
	}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PatchAuthority {
	Global,
	Project,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
enum PatchValue<T> {
	Set(T),
	Clear(ClearValue),
}

impl<T> PatchValue<T> {
	fn into_option(self) -> Option<T> {
		match self {
			Self::Set(value) => Some(value),
			Self::Clear(_) => None,
		}
	}
}

#[derive(Debug, Clone)]
struct ClearValue;

impl<'de> Deserialize<'de> for ClearValue {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: serde::Deserializer<'de>,
	{
		#[derive(Deserialize)]
		#[serde(deny_unknown_fields)]
		struct Wire {
			clear: bool,
		}

		let wire = Wire::deserialize(deserializer)?;
		if wire.clear {
			Ok(Self)
		} else {
			Err(serde::de::Error::custom("clear marker must be true"))
		}
	}
}

fn project_root(start: &Path) -> Option<PathBuf> {
	let start = fs::canonicalize(start).ok()?;
	start
		.ancestors()
		.find(|candidate| candidate.join(".git").exists())
		.map(Path::to_path_buf)
}

#[cfg(test)]
#[path = "tests.rs"]
mod tests;