-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathconfig_io.rs
More file actions
506 lines (462 loc) · 18.4 KB
/
config_io.rs
File metadata and controls
506 lines (462 loc) · 18.4 KB
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
use {
super::*,
crate::{env_subst::substitute_env, schema::MoltisConfig},
std::{
path::{Path, PathBuf},
sync::Mutex,
},
tracing::{debug, info, warn},
};
/// Load config from the given path (any supported format).
///
/// After parsing, `MOLTIS_*` env vars are applied as overrides.
pub fn load_config(path: &Path) -> crate::Result<MoltisConfig> {
let raw = std::fs::read_to_string(path).map_err(|source| {
crate::Error::external(format!("failed to read {}", path.display()), source)
})?;
let raw = substitute_env(&raw);
let config = parse_config(&raw, path)?;
Ok(apply_env_overrides(config))
}
/// Load and parse the config file with env substitution and includes.
pub fn load_config_value(path: &Path) -> crate::Result<serde_json::Value> {
let raw = std::fs::read_to_string(path).map_err(|source| {
crate::Error::external(format!("failed to read {}", path.display()), source)
})?;
let raw = substitute_env(&raw);
parse_config_value(&raw, path)
}
/// Discover and load config from standard locations.
///
/// Search order:
/// 1. `./moltis.{toml,yaml,yml,json}` (project-local)
/// 2. `~/.config/moltis/moltis.{toml,yaml,yml,json}` (user-global)
///
/// Returns `MoltisConfig::default()` if no config file is found.
///
/// If the config has port 0 (either from defaults or missing `[server]` section),
/// a random available port is generated and saved to the config file.
pub fn discover_and_load() -> MoltisConfig {
let mut cfg = if let Some(path) = find_config_file() {
debug!(path = %path.display(), "loading config");
match load_config(&path) {
Ok(mut cfg) => {
// If port is 0 (default/missing), generate a random port and save it.
// Use `save_config_to_path` directly instead of `save_config` because
// this function may be called from within `update_config`, which already
// holds `CONFIG_SAVE_LOCK`. Re-acquiring a `std::sync::Mutex` on the
// same thread would deadlock.
if cfg.server.port == 0 {
cfg.server.port = generate_random_port();
debug!(
port = cfg.server.port,
"generated random port for existing config"
);
if let Err(e) = save_config_to_path(&path, &cfg) {
warn!(error = %e, "failed to save config with generated port");
}
}
cfg // env overrides already applied by load_config
},
Err(e) => {
warn!(path = %path.display(), error = %e, "failed to load config, using defaults");
apply_env_overrides(MoltisConfig::default())
},
}
} else {
let default_path = find_or_default_config_path();
debug!(
path = %default_path.display(),
"no config file found, writing default config with random port"
);
let mut config = MoltisConfig::default();
// Generate a unique port for this installation
config.server.port = generate_random_port();
if let Err(e) = write_default_config(&default_path, &config) {
warn!(
path = %default_path.display(),
error = %e,
"failed to write default config file, continuing with in-memory defaults"
);
} else {
info!(
path = %default_path.display(),
"wrote default config template"
);
}
apply_env_overrides(config)
};
// Merge markdown agent definitions (TOML presets take precedence).
let agent_defs = crate::agent_defs::discover_agent_defs();
if !agent_defs.is_empty() {
debug!(
count = agent_defs.len(),
"discovered markdown agent definitions"
);
crate::agent_defs::merge_agent_defs(&mut cfg.agents.presets, agent_defs);
}
cfg
}
/// Find the first config file in standard locations.
///
/// When a config dir override is set, only that directory is searched —
/// project-local and user-global paths are skipped for isolation.
pub fn find_config_file() -> Option<PathBuf> {
if let Some(dir) = config_dir_override() {
for name in CONFIG_FILENAMES {
let p = dir.join(name);
if p.exists() {
return Some(p);
}
}
// Override is set — don't fall through to other locations.
return None;
}
// Project-local
for name in CONFIG_FILENAMES {
let p = PathBuf::from(name);
if p.exists() {
return Some(p);
}
}
// User-global: ~/.config/moltis/
if let Some(dir) = home_dir().map(|h| h.join(".config").join("moltis")) {
for name in CONFIG_FILENAMES {
let p = dir.join(name);
if p.exists() {
return Some(p);
}
}
}
None
}
pub fn find_or_default_config_path() -> PathBuf {
if let Some(path) = find_config_file() {
return path;
}
config_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("moltis.toml")
}
/// Lock guarding config read-modify-write cycles.
struct ConfigSaveState {
target_path: Option<PathBuf>,
}
/// Lock guarding config read-modify-write cycles and the target config path
/// being synchronized.
static CONFIG_SAVE_LOCK: Mutex<ConfigSaveState> = Mutex::new(ConfigSaveState { target_path: None });
/// Atomically load the current config, apply `f`, and save.
///
/// Acquires a process-wide lock so concurrent callers cannot race.
/// Returns the path written to.
pub fn update_config(f: impl FnOnce(&mut MoltisConfig)) -> crate::Result<PathBuf> {
let mut guard = CONFIG_SAVE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let target_path = find_or_default_config_path();
guard.target_path = Some(target_path.clone());
let mut config = discover_and_load();
f(&mut config);
save_config_to_path(&target_path, &config)
}
/// Serialize `config` to TOML and write it to the user-global config path.
///
/// Creates parent directories if needed. Returns the path written to.
///
/// Prefer [`update_config`] for read-modify-write cycles to avoid races.
pub fn save_config(config: &MoltisConfig) -> crate::Result<PathBuf> {
let mut guard = CONFIG_SAVE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let target_path = find_or_default_config_path();
guard.target_path = Some(target_path.clone());
save_config_to_path(&target_path, config)
}
/// Write raw TOML to the config file, preserving comments.
///
/// Validates the input by parsing it first. Acquires the config save lock
/// so concurrent callers cannot race. Returns the path written to.
pub fn save_raw_config(toml_str: &str) -> crate::Result<PathBuf> {
let _: MoltisConfig = toml::from_str(toml_str)
.map_err(|source| crate::Error::external(format!("invalid config: {source}"), source))?;
let mut guard = CONFIG_SAVE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let path = find_or_default_config_path();
guard.target_path = Some(path.clone());
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(&path, toml_str)?;
debug!(path = %path.display(), "saved raw config");
Ok(path)
}
/// Serialize `config` to TOML and write it to the provided path.
///
/// For existing TOML files, this preserves user comments by merging the new
/// serialized values into the current document structure before writing.
pub fn save_config_to_path(path: &Path, config: &MoltisConfig) -> crate::Result<PathBuf> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let toml_str = toml::to_string_pretty(config)
.map_err(|source| crate::Error::external("serialize config", source))?;
let is_toml_path = path
.extension()
.and_then(|ext| ext.to_str())
.is_some_and(|ext| ext.eq_ignore_ascii_case("toml"));
if is_toml_path && path.exists() {
if let Err(error) = merge_toml_preserving_comments(path, &toml_str) {
warn!(
path = %path.display(),
error = %error,
"failed to preserve TOML comments, rewriting config without comments"
);
std::fs::write(path, toml_str)?;
}
} else {
std::fs::write(path, toml_str)?;
}
debug!(path = %path.display(), "saved config");
Ok(path.to_path_buf())
}
fn merge_toml_preserving_comments(path: &Path, updated_toml: &str) -> crate::Result<()> {
let current_toml = std::fs::read_to_string(path)?;
let mut current_doc = current_toml
.parse::<toml_edit::DocumentMut>()
.map_err(|source| crate::Error::external("parse existing TOML", source))?;
let updated_doc = updated_toml
.parse::<toml_edit::DocumentMut>()
.map_err(|source| crate::Error::external("parse updated TOML", source))?;
merge_toml_tables(current_doc.as_table_mut(), updated_doc.as_table());
std::fs::write(path, current_doc.to_string())?;
Ok(())
}
pub(super) fn merge_toml_tables(current: &mut toml_edit::Table, updated: &toml_edit::Table) {
let current_keys: Vec<String> = current.iter().map(|(key, _)| key.to_string()).collect();
for key in current_keys {
if !updated.contains_key(&key) {
let _ = current.remove(&key);
}
}
for (key, updated_item) in updated.iter() {
if let Some(current_item) = current.get_mut(key) {
merge_toml_items(current_item, updated_item);
} else {
// Clone the item and strip `doc_position` metadata inherited from
// the source document. Without this, toml_edit uses the position
// from the *serialized* document, causing new sub-tables to be
// interleaved among existing sections instead of appearing after
// their parent (GH-684).
current.insert(key, clone_item_without_positions(updated_item));
}
}
}
/// Deep-clone a `toml_edit::Item`, stripping `doc_position` from every table
/// so that newly inserted entries get auto-positioned by `toml_edit` rather
/// than inheriting stale positions from a different document.
fn clone_item_without_positions(item: &toml_edit::Item) -> toml_edit::Item {
match item {
toml_edit::Item::Table(t) => toml_edit::Item::Table(clone_table_without_positions(t)),
toml_edit::Item::ArrayOfTables(arr) => {
let mut new_arr = toml_edit::ArrayOfTables::new();
for table in arr.iter() {
new_arr.push(clone_table_without_positions(table));
}
toml_edit::Item::ArrayOfTables(new_arr)
},
other => other.clone(),
}
}
/// Clone a table, recursively stripping `doc_position` so new tables get
/// auto-positioned when inserted into a different document.
fn clone_table_without_positions(src: &toml_edit::Table) -> toml_edit::Table {
let mut dst = toml_edit::Table::new();
// doc_position is None for manually created tables → auto-positioned
dst.set_implicit(src.is_implicit());
dst.set_dotted(src.is_dotted());
*dst.decor_mut() = src.decor().clone();
for (key, item) in src.iter() {
dst.insert(key, clone_item_without_positions(item));
// Preserve key decorations (whitespace/comments around the key)
if let (Some(src_key), Some(mut dst_key)) = (src.key(key), dst.key_mut(key)) {
*dst_key.leaf_decor_mut() = src_key.leaf_decor().clone();
*dst_key.dotted_decor_mut() = src_key.dotted_decor().clone();
}
}
dst
}
fn merge_toml_items(current: &mut toml_edit::Item, updated: &toml_edit::Item) {
match (current, updated) {
(toml_edit::Item::Table(current_table), toml_edit::Item::Table(updated_table)) => {
merge_toml_tables(current_table, updated_table);
},
(toml_edit::Item::Value(current_value), toml_edit::Item::Value(updated_value)) => {
let existing_decor = current_value.decor().clone();
*current_value = updated_value.clone();
*current_value.decor_mut() = existing_decor;
},
(current_item, updated_item) => {
*current_item = updated_item.clone();
},
}
}
/// Write the default config file to the user-global config path.
/// Only called when no config file exists yet.
/// Uses a comprehensive template with all options documented.
pub(super) fn write_default_config(path: &Path, config: &MoltisConfig) -> crate::Result<()> {
if path.exists() {
return Ok(());
}
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
// Use the documented template instead of plain serialization
let toml_str = crate::template::default_config_template(config.server.port);
std::fs::write(path, &toml_str)?;
debug!(path = %path.display(), "wrote default config file with template");
Ok(())
}
/// Apply `MOLTIS_*` environment variable overrides to a loaded config.
///
/// Maps env vars to config fields using `__` as a section separator and
/// lowercasing. For example:
/// - `MOLTIS_AUTH_DISABLED=true` → `auth.disabled = true`
/// - `MOLTIS_TOOLS_EXEC_DEFAULT_TIMEOUT_SECS=60` → `tools.exec.default_timeout_secs = 60`
/// - `MOLTIS_CHAT_MESSAGE_QUEUE_MODE=collect` → `chat.message_queue_mode = "collect"`
///
/// The config is serialized to a JSON value, env overrides are merged in,
/// then deserialized back. Only env vars with the `MOLTIS_` prefix are
/// considered. `MOLTIS_CONFIG_DIR`, `MOLTIS_DATA_DIR`, `MOLTIS_SHARE_DIR`,
/// `MOLTIS_ASSETS_DIR`, `MOLTIS_TOKEN`, `MOLTIS_PASSWORD`, `MOLTIS_TAILSCALE`,
/// `MOLTIS_WEBAUTHN_RP_ID`, and `MOLTIS_WEBAUTHN_ORIGIN` are excluded
/// (they are handled separately).
pub fn apply_env_overrides(config: MoltisConfig) -> MoltisConfig {
apply_env_overrides_with(config, std::env::vars())
}
/// Apply env overrides from an arbitrary iterator of (key, value) pairs.
/// Exposed for testing without mutating the process environment.
pub(super) fn apply_env_overrides_with(
config: MoltisConfig,
vars: impl Iterator<Item = (String, String)>,
) -> MoltisConfig {
use serde_json::Value;
const EXCLUDED: &[&str] = &[
"MOLTIS_CONFIG_DIR",
"MOLTIS_DATA_DIR",
"MOLTIS_SHARE_DIR",
"MOLTIS_ASSETS_DIR",
"MOLTIS_TOKEN",
"MOLTIS_PASSWORD",
"MOLTIS_TAILSCALE",
"MOLTIS_WEBAUTHN_RP_ID",
"MOLTIS_WEBAUTHN_ORIGIN",
"MOLTIS_EXTERNAL_URL",
];
let mut root: Value = match serde_json::to_value(&config) {
Ok(v) => v,
Err(e) => {
warn!(error = %e, "failed to serialize config for env override");
return config;
},
};
for (key, val) in vars {
if !key.starts_with("MOLTIS_") {
continue;
}
if EXCLUDED.contains(&key.as_str()) {
continue;
}
// MOLTIS_AUTH__DISABLED → ["auth", "disabled"]
let path_parts: Vec<String> = key["MOLTIS_".len()..]
.split("__")
.map(|segment| segment.to_lowercase())
.collect();
if path_parts.is_empty() {
continue;
}
// Navigate to the parent object and set the leaf value.
let parsed_val = parse_env_value(&val);
set_nested(&mut root, &path_parts, parsed_val);
}
match serde_json::from_value(root) {
Ok(cfg) => cfg,
Err(e) => {
warn!(error = %e, "failed to apply env overrides, using config as-is");
config
},
}
}
/// Parse a string env value into a JSON value, trying bool and number first.
pub(super) fn parse_env_value(val: &str) -> serde_json::Value {
let trimmed = val.trim();
// Support JSON arrays/objects for list-like env overrides, e.g.
// MOLTIS_PROVIDERS__OFFERED='["openai","github-copilot"]' or '[]'.
if ((trimmed.starts_with('[') && trimmed.ends_with(']'))
|| (trimmed.starts_with('{') && trimmed.ends_with('}')))
&& let Ok(parsed) = serde_json::from_str::<serde_json::Value>(trimmed)
{
return parsed;
}
if val.eq_ignore_ascii_case("true") {
return serde_json::Value::Bool(true);
}
if val.eq_ignore_ascii_case("false") {
return serde_json::Value::Bool(false);
}
if let Ok(n) = val.parse::<i64>() {
return serde_json::Value::Number(n.into());
}
if let Ok(n) = val.parse::<f64>()
&& let Some(n) = serde_json::Number::from_f64(n)
{
return serde_json::Value::Number(n);
}
serde_json::Value::String(val.to_string())
}
/// Set a value at a nested JSON path, creating intermediate objects as needed.
pub(super) fn set_nested(root: &mut serde_json::Value, path: &[String], val: serde_json::Value) {
if path.is_empty() {
return;
}
let mut current = root;
for (i, key) in path.iter().enumerate() {
if i == path.len() - 1 {
if let serde_json::Value::Object(map) = current {
map.insert(key.clone(), val);
}
return;
}
if !current.get(key).is_some_and(|v| v.is_object())
&& let serde_json::Value::Object(map) = current
{
map.insert(key.clone(), serde_json::Value::Object(Default::default()));
}
let Some(next) = current.get_mut(key) else {
return;
};
current = next;
}
}
pub(super) fn parse_config(raw: &str, path: &Path) -> crate::Result<MoltisConfig> {
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("toml");
match ext {
"toml" => Ok(toml::from_str(raw)?),
"yaml" | "yml" => Ok(serde_yaml::from_str(raw)?),
"json" => Ok(serde_json::from_str(raw)?),
_ => Err(crate::Error::message(format!(
"unsupported config format: .{ext}"
))),
}
}
fn parse_config_value(raw: &str, path: &Path) -> crate::Result<serde_json::Value> {
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("toml");
match ext {
"toml" => {
let v: toml::Value = toml::from_str(raw)?;
Ok(serde_json::to_value(v)?)
},
"yaml" | "yml" => {
let v: serde_yaml::Value = serde_yaml::from_str(raw)?;
Ok(serde_json::to_value(v)?)
},
"json" => Ok(serde_json::from_str(raw)?),
_ => Err(crate::Error::message(format!(
"unsupported config format: .{ext}"
))),
}
}