clawft-platform 0.8.3

Platform abstraction layer for clawft
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
//! Configuration file discovery and loading.
//!
//! Ports the config discovery algorithm from Python `nanobot/config/loader.py`.
//!
//! The discovery order is:
//! 1. `CLAWFT_CONFIG` environment variable (absolute path).
//! 2. `~/.clawft/config.json`
//! 3. `~/.nanobot/config.json` (legacy fallback).
//! 4. If none found, return an empty JSON object (`{}`).
//!
//! JSON keys are normalized from camelCase to snake_case before returning,
//! matching the Python behavior where Pydantic models use snake_case field names.

use std::path::PathBuf;

use serde_json::Value;

/// Discover the config file path using the fallback chain.
///
/// Returns `None` if no config file exists at any of the candidate locations.
/// The discovery order is:
/// 1. Path from `CLAWFT_CONFIG` environment variable.
/// 2. `~/.clawft/config.json`
/// 3. `~/.nanobot/config.json`
///
/// On native targets, candidate paths are checked for existence using
/// synchronous `Path::exists()`. On non-native targets (WASM), the first
/// candidate path is returned without a filesystem existence check --
/// the caller's async `fs.exists()` handles validation.
pub fn discover_config_path(
    env: &dyn super::env::Environment,
    home_dir: Option<PathBuf>,
) -> Option<PathBuf> {
    // Step 1: Check CLAWFT_CONFIG env var
    if let Some(env_path) = env.get_var("CLAWFT_CONFIG") {
        let path = PathBuf::from(env_path);
        return Some(path);
    }

    // Step 2 & 3: Check home directory paths
    if let Some(home) = home_dir {
        let clawft_path = home.join(".clawft").join("config.json");

        #[cfg(feature = "native")]
        {
            if clawft_path.exists() {
                return Some(clawft_path);
            }

            let nanobot_path = home.join(".nanobot").join("config.json");
            if nanobot_path.exists() {
                return Some(nanobot_path);
            }
        }

        // On non-native (WASM) targets, return the preferred path without
        // synchronous filesystem checks. The caller validates asynchronously.
        #[cfg(not(feature = "native"))]
        {
            return Some(clawft_path);
        }
    }

    None
}

/// Split configuration layers produced by the discovery algorithm.
///
/// WEFT-10 / FIX-04: the workspace overlay is kept separate from the
/// global (weave.toml + home JSON) layer so callers can construct
/// `PermissionResolver::new(global, Some(workspace))` and let
/// `enforce_workspace_ceiling` clamp elevated workspace grants against
/// system-wide bounds. Deep-merging first bypasses the ceiling.
#[derive(Debug, Clone)]
pub struct ConfigLayers {
    /// Layers 1+2 only: `weave.toml` + home/env JSON. No workspace overlay.
    pub global: Value,
    /// Layer 3 alone: cwd `.clawft/config.json`, when present and parseable.
    pub workspace: Option<Value>,
}

impl ConfigLayers {
    /// Deep-merge workspace onto global (legacy single-config behaviour).
    ///
    /// Use this for non-security fields (agents, providers, tools) where
    /// most-specific-wins overlay is correct. For `routing.permissions`,
    /// prefer keeping the layers split and handing them to
    /// `PermissionResolver`.
    pub fn merged(&self) -> Value {
        let mut out = self.global.clone();
        if let Some(ref ws) = self.workspace {
            deep_merge(&mut out, ws);
        }
        out
    }

    /// True when a workspace overlay was loaded.
    pub fn has_workspace(&self) -> bool {
        self.workspace.is_some()
    }
}

/// Load configuration as split global / workspace layers (WEFT-10).
///
/// Layer order:
/// 1. `weave.toml` in the current directory (project-level settings)
/// 2. JSON config from the discovery chain (`CLAWFT_CONFIG` /
///    `~/.clawft/config.json` / `~/.nanobot/config.json`)
/// 3. Workspace overlay at cwd `.clawft/config.json` (returned separately)
///
/// Layers 1+2 are deep-merged into [`ConfigLayers::global`]. Layer 3 is
/// returned as [`ConfigLayers::workspace`] without being merged, so
/// multi-tenant permission ceilings can be enforced.
pub async fn load_config_layers(
    fs: &dyn super::fs::FileSystem,
    env: &dyn super::env::Environment,
) -> Result<ConfigLayers, Box<dyn std::error::Error + Send + Sync>> {
    // Layer 1: weave.toml from project root (current directory).
    let mut global = load_weave_toml(fs)
        .await
        .unwrap_or_else(|_| Value::Object(serde_json::Map::new()));

    // Layer 2: JSON config from discovery chain (overrides weave.toml).
    let home = fs.home_dir();
    let json_path = discover_config_path(env, home);

    if let Some(path) = json_path
        && fs.exists(&path).await
    {
        tracing::debug!(path = %path.display(), "loading JSON config");
        match fs.read_to_string(&path).await {
            Ok(contents) => match serde_json::from_str::<Value>(&contents) {
                Ok(json_value) => {
                    let normalized = normalize_keys(json_value);
                    deep_merge(&mut global, &normalized);
                }
                Err(e) => {
                    tracing::warn!(
                        path = %path.display(),
                        error = %e,
                        "failed to parse JSON config, using weave.toml only"
                    );
                }
            },
            Err(e) => {
                tracing::warn!(
                    path = %path.display(),
                    error = %e,
                    "failed to read JSON config"
                );
            }
        }
    }

    // Layer 3: workspace JSON overlay from cwd `.clawft/config.json`.
    // Kept separate (WEFT-10) so PermissionResolver can apply the
    // security ceiling. Most-specific wins for non-security merge via
    // `ConfigLayers::merged()`.
    let workspace_path = PathBuf::from(".clawft").join("config.json");
    let mut workspace = None;
    if fs.exists(&workspace_path).await {
        tracing::debug!(
            path = %workspace_path.display(),
            "loading workspace config overlay (kept split for permission ceiling)"
        );
        match fs.read_to_string(&workspace_path).await {
            Ok(contents) => match serde_json::from_str::<Value>(&contents) {
                Ok(json_value) => {
                    workspace = Some(normalize_keys(json_value));
                }
                Err(e) => tracing::warn!(
                    path = %workspace_path.display(),
                    error = %e,
                    "failed to parse workspace config; ignoring overlay"
                ),
            },
            Err(e) => tracing::warn!(
                path = %workspace_path.display(),
                error = %e,
                "failed to read workspace config; ignoring overlay"
            ),
        }
    }

    if global.as_object().is_none_or(|m| m.is_empty()) && workspace.is_none() {
        tracing::info!("no config found (checked weave.toml + JSON), using defaults");
    }

    Ok(ConfigLayers { global, workspace })
}

/// Load raw JSON configuration using the discovery algorithm.
///
/// Convenience wrapper around [`load_config_layers`] that deep-merges
/// the workspace overlay onto the global layer (legacy single-`Value`
/// behaviour). Prefer [`load_config_layers`] when constructing a
/// `PermissionResolver` so the workspace ceiling can run.
///
/// Returns the merged and key-normalized JSON value. The caller (typically
/// `clawft-types`) deserializes this into a typed `Config` struct.
pub async fn load_config_raw(
    fs: &dyn super::fs::FileSystem,
    env: &dyn super::env::Environment,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
    Ok(load_config_layers(fs, env).await?.merged())
}

/// Load `weave.toml` from the current directory if it exists.
async fn load_weave_toml(
    fs: &dyn super::fs::FileSystem,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
    load_weave_toml_file(fs, &PathBuf::from("weave.toml")).await
}

/// Load a `weave.toml` at `toml_path` as key-normalized JSON.
///
/// A missing file is an empty object; an unreadable or unparseable one is
/// an error (the caller decides whether that is fatal).
pub async fn load_weave_toml_file(
    fs: &dyn super::fs::FileSystem,
    toml_path: &std::path::Path,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
    if !fs.exists(toml_path).await {
        return Ok(Value::Object(serde_json::Map::new()));
    }

    tracing::debug!(path = %toml_path.display(), "loading weave.toml");
    let contents = fs
        .read_to_string(toml_path)
        .await
        .map_err(|e| format!("failed to read weave.toml: {e}"))?;

    let toml_value: toml::Value = contents
        .parse()
        .map_err(|e| format!("failed to parse weave.toml: {e}"))?;

    // Convert TOML Value to serde_json Value for uniform handling.
    let json_str = serde_json::to_string(&toml_value)
        .map_err(|e| format!("failed to convert weave.toml to JSON: {e}"))?;
    let json_value: Value = serde_json::from_str(&json_str)
        .map_err(|e| format!("failed to re-parse weave.toml as JSON: {e}"))?;

    Ok(normalize_keys(json_value))
}

/// Deep-merge `overlay` into `base`. Overlay values win on conflict.
/// Objects are merged recursively; non-object values are replaced.
pub fn deep_merge(base: &mut Value, overlay: &Value) {
    match (base, overlay) {
        (Value::Object(base_map), Value::Object(overlay_map)) => {
            for (key, overlay_val) in overlay_map {
                let entry = base_map.entry(key.clone()).or_insert(Value::Null);
                deep_merge(entry, overlay_val);
            }
        }
        (base, overlay) => {
            if !overlay.is_null() {
                *base = overlay.clone();
            }
        }
    }
}

/// Convert camelCase JSON keys to snake_case recursively.
///
/// Processes objects and arrays recursively. Non-object/array values are
/// returned unchanged. This matches the Python `convert_keys()` function
/// from `nanobot/config/loader.py`.
pub fn normalize_keys(value: Value) -> Value {
    match value {
        Value::Object(map) => {
            let mut new_map = serde_json::Map::new();
            for (key, val) in map {
                let snake_key = camel_to_snake(&key);
                new_map.insert(snake_key, normalize_keys(val));
            }
            Value::Object(new_map)
        }
        Value::Array(arr) => Value::Array(arr.into_iter().map(normalize_keys).collect()),
        other => other,
    }
}

/// Convert a single camelCase string to snake_case.
///
/// Handles consecutive uppercase letters (acronyms) correctly:
/// a run of uppercase letters like `"HTML"` is kept together, with an
/// underscore inserted only before the last uppercase letter if it is
/// followed by a lowercase letter (indicating the start of a new word).
///
/// # Examples
/// ```
/// # use clawft_platform::config_loader::camel_to_snake;
/// assert_eq!(camel_to_snake("camelCase"), "camel_case");
/// assert_eq!(camel_to_snake("systemPrompt"), "system_prompt");
/// assert_eq!(camel_to_snake("already_snake"), "already_snake");
/// assert_eq!(camel_to_snake("HTMLParser"), "html_parser");
/// assert_eq!(camel_to_snake("getHTMLParser"), "get_html_parser");
/// assert_eq!(camel_to_snake("simpleXML"), "simple_xml");
/// ```
pub fn camel_to_snake(name: &str) -> String {
    let chars: Vec<char> = name.chars().collect();
    let mut result = String::with_capacity(name.len() + 4);

    for (i, &ch) in chars.iter().enumerate() {
        if ch.is_uppercase() && i > 0 {
            let prev = chars[i - 1];
            let next = chars.get(i + 1).copied();

            // Insert underscore before:
            // 1. An uppercase letter preceded by a lowercase letter (camelCase boundary)
            // 2. An uppercase letter followed by a lowercase letter, when preceded
            //    by an uppercase letter (end of acronym: "HTMLParser" -> "html_parser")
            if prev.is_lowercase()
                || (prev.is_uppercase() && next.is_some_and(|c| c.is_lowercase()))
            {
                result.push('_');
            }
        }
        result.push(ch.to_ascii_lowercase());
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // ── camel_to_snake tests ──────────────────────────────────────────

    #[test]
    fn test_camel_to_snake_basic() {
        assert_eq!(camel_to_snake("camelCase"), "camel_case");
    }

    #[test]
    fn test_camel_to_snake_multiple_words() {
        assert_eq!(camel_to_snake("systemPrompt"), "system_prompt");
        assert_eq!(camel_to_snake("contextWindow"), "context_window");
        assert_eq!(
            camel_to_snake("memoryConsolidation"),
            "memory_consolidation"
        );
    }

    #[test]
    fn test_camel_to_snake_already_snake() {
        assert_eq!(camel_to_snake("already_snake"), "already_snake");
    }

    #[test]
    fn test_camel_to_snake_single_word() {
        assert_eq!(camel_to_snake("model"), "model");
    }

    #[test]
    fn test_camel_to_snake_all_upper() {
        // Pure acronym stays together (no trailing lowercase to split on)
        assert_eq!(camel_to_snake("HTML"), "html");
    }

    #[test]
    fn test_camel_to_snake_acronym_then_word() {
        // Acronym followed by a word: split before the last uppercase
        assert_eq!(camel_to_snake("HTMLParser"), "html_parser");
        assert_eq!(camel_to_snake("getHTMLParser"), "get_html_parser");
        assert_eq!(camel_to_snake("simpleXML"), "simple_xml");
        assert_eq!(camel_to_snake("XMLHTTPRequest"), "xmlhttp_request");
    }

    #[test]
    fn test_camel_to_snake_leading_upper() {
        // Leading uppercase is just lowered, no underscore
        assert_eq!(camel_to_snake("Config"), "config");
    }

    #[test]
    fn test_camel_to_snake_empty() {
        assert_eq!(camel_to_snake(""), "");
    }

    // ── normalize_keys tests ──────────────────────────────────────────

    #[test]
    fn test_normalize_keys_flat_object() {
        let input = json!({
            "systemPrompt": "hello",
            "contextWindow": 4096
        });
        let expected = json!({
            "system_prompt": "hello",
            "context_window": 4096
        });
        assert_eq!(normalize_keys(input), expected);
    }

    #[test]
    fn test_normalize_keys_nested_object() {
        let input = json!({
            "agentsConfig": {
                "defaultModel": "gpt-4",
                "maxTokens": 1024
            }
        });
        let expected = json!({
            "agents_config": {
                "default_model": "gpt-4",
                "max_tokens": 1024
            }
        });
        assert_eq!(normalize_keys(input), expected);
    }

    #[test]
    fn test_normalize_keys_array() {
        let input = json!([
            {"firstName": "Alice"},
            {"firstName": "Bob"}
        ]);
        let expected = json!([
            {"first_name": "Alice"},
            {"first_name": "Bob"}
        ]);
        assert_eq!(normalize_keys(input), expected);
    }

    #[test]
    fn test_normalize_keys_primitives_unchanged() {
        assert_eq!(normalize_keys(json!(42)), json!(42));
        assert_eq!(normalize_keys(json!("hello")), json!("hello"));
        assert_eq!(normalize_keys(json!(true)), json!(true));
        assert_eq!(normalize_keys(json!(null)), json!(null));
    }

    #[test]
    fn test_normalize_keys_empty_object() {
        assert_eq!(normalize_keys(json!({})), json!({}));
    }

    // ── discover_config_path tests ────────────────────────────────────

    /// A minimal mock environment for config discovery tests.
    struct MockEnv {
        vars: std::collections::HashMap<String, String>,
    }

    impl MockEnv {
        fn new() -> Self {
            Self {
                vars: std::collections::HashMap::new(),
            }
        }

        fn with_var(mut self, key: &str, value: &str) -> Self {
            self.vars.insert(key.to_string(), value.to_string());
            self
        }
    }

    impl super::super::env::Environment for MockEnv {
        fn get_var(&self, name: &str) -> Option<String> {
            self.vars.get(name).cloned()
        }

        fn set_var(&self, _name: &str, _value: &str) {
            // No-op for mock
        }

        fn remove_var(&self, _name: &str) {
            // No-op for mock
        }
    }

    #[test]
    fn test_discover_env_var_takes_precedence() {
        let env = MockEnv::new().with_var("CLAWFT_CONFIG", "/custom/config.json");
        let result = discover_config_path(&env, Some(PathBuf::from("/home/user")));
        assert_eq!(result, Some(PathBuf::from("/custom/config.json")));
    }

    #[test]
    fn test_discover_no_home_no_env() {
        let env = MockEnv::new();
        let result = discover_config_path(&env, None);
        assert_eq!(result, None);
    }

    /// Existence checks for home candidates are `#[cfg(feature = "native")]`
    /// only; browser/WASM returns the preferred path without probing the
    /// host FS (see `discover_config_path`).
    #[cfg(feature = "native")]
    #[test]
    fn test_discover_home_but_no_files() {
        // When home dir is given but no config files exist on disk,
        // discover returns None (both .clawft and .nanobot paths don't exist).
        let env = MockEnv::new();
        // Use a path that definitely does not contain config files.
        let result = discover_config_path(
            &env,
            Some(PathBuf::from("/tmp/clawft_test_nonexistent_home")),
        );
        assert_eq!(result, None);
    }

    // ── Workspace overlay tests (Layer 3 of load_config_raw) ─────────
    //
    // The minimal MockFs below only implements the methods load_config_raw
    // calls (`exists`, `read_to_string`, `home_dir`). Every other method
    // panics with `unimplemented!()` so an accidental call in a future
    // refactor surfaces immediately instead of silently no-oping.

    use async_trait::async_trait;
    use std::collections::HashMap;
    use std::path::Path;
    use std::sync::Mutex;

    struct MockFs {
        home: Option<PathBuf>,
        files: Mutex<HashMap<PathBuf, String>>,
    }

    impl MockFs {
        fn new(home: Option<PathBuf>) -> Self {
            Self {
                home,
                files: Mutex::new(HashMap::new()),
            }
        }

        fn with_file(self, path: impl Into<PathBuf>, contents: &str) -> Self {
            self.files
                .lock()
                .unwrap()
                .insert(path.into(), contents.to_string());
            self
        }
    }

    // Match FileSystem's Send bound: browser uses ?Send (WEFT-391 test path).
    #[cfg_attr(not(feature = "browser"), async_trait)]
    #[cfg_attr(feature = "browser", async_trait(?Send))]
    impl super::super::fs::FileSystem for MockFs {
        async fn read_to_string(&self, path: &Path) -> std::io::Result<String> {
            self.files
                .lock()
                .unwrap()
                .get(path)
                .cloned()
                .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "not found"))
        }
        async fn write_string(&self, _: &Path, _: &str) -> std::io::Result<()> {
            unimplemented!()
        }
        async fn append_string(&self, _: &Path, _: &str) -> std::io::Result<()> {
            unimplemented!()
        }
        async fn exists(&self, path: &Path) -> bool {
            self.files.lock().unwrap().contains_key(path)
        }
        async fn list_dir(&self, _: &Path) -> std::io::Result<Vec<PathBuf>> {
            unimplemented!()
        }
        async fn create_dir_all(&self, _: &Path) -> std::io::Result<()> {
            unimplemented!()
        }
        async fn remove_file(&self, _: &Path) -> std::io::Result<()> {
            unimplemented!()
        }
        fn home_dir(&self) -> Option<PathBuf> {
            self.home.clone()
        }
        async fn metadata(
            &self,
            path: &Path,
        ) -> std::io::Result<super::super::fs::FsMetadata> {
            if self.exists(path).await {
                Ok(super::super::fs::FsMetadata {
                    is_dir: false,
                    len: self
                        .files
                        .lock()
                        .unwrap()
                        .get(path)
                        .map(|s| s.len() as u64)
                        .unwrap_or(0),
                })
            } else {
                Err(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    "not found",
                ))
            }
        }
    }

    fn workspace_config_path() -> PathBuf {
        PathBuf::from(".clawft").join("config.json")
    }

    // The home-layer (Layer 2) goes through `discover_config_path`,
    // which uses a sync `Path::exists()` against the *real* filesystem
    // — it cannot be exercised with a mock `FileSystem`. These tests
    // therefore focus on the new workspace overlay (Layer 3), which
    // does flow through `fs.exists()` / `fs.read_to_string()` and is
    // mockable. End-to-end behaviour (workspace overrides home with
    // both layers populated) is covered by the live smoke test.

    #[tokio::test]
    async fn workspace_overlay_applied_when_present() {
        // Workspace declares an agent.chat channel at level 2; with
        // no other layer present, the overlay must surface it.
        let workspace = r#"{
            "routing": {
                "permissions": {
                    "channels": { "agent.chat": { "level": 2 } }
                }
            }
        }"#;
        // Home dir set to a path that won't exist on the real fs so
        // discover_config_path returns None (Layer 2 inert).
        let fs = MockFs::new(Some(PathBuf::from("/tmp/clawft_test_no_home_xyz")))
            .with_file(workspace_config_path(), workspace);
        let env = MockEnv::new();
        let merged = load_config_raw(&fs, &env).await.unwrap();
        let lvl = merged
            .pointer("/routing/permissions/channels/agent.chat/level")
            .and_then(Value::as_u64)
            .expect("level present after overlay");
        assert_eq!(lvl, 2);
    }

    #[tokio::test]
    async fn workspace_overlay_skipped_when_absent() {
        // No workspace config + no home config (real fs path missing)
        // = empty merged result, behaviour matches pre-fix.
        let fs = MockFs::new(Some(PathBuf::from("/tmp/clawft_test_no_home_xyz")));
        let env = MockEnv::new();
        let merged = load_config_raw(&fs, &env).await.unwrap();
        assert!(
            merged.as_object().map(|m| m.is_empty()).unwrap_or(true),
            "no layers present should yield empty merged config"
        );
    }

    #[tokio::test]
    async fn workspace_overlay_invalid_json_is_ignored() {
        // Malformed workspace JSON must not abort load_config_raw —
        // the overlay is best-effort. Other layers continue to apply.
        let fs = MockFs::new(Some(PathBuf::from("/tmp/clawft_test_no_home_xyz")))
            .with_file(workspace_config_path(), "{ this is not json");
        let env = MockEnv::new();
        let merged = load_config_raw(&fs, &env).await.unwrap();
        assert!(
            merged.is_object(),
            "loader returns ok despite parse failure"
        );
    }

    #[tokio::test]
    async fn workspace_overlay_keys_are_normalized_to_snake_case() {
        // Workspace JSON uses camelCase (matches the existing config
        // file convention); load_config_raw normalizes via
        // `normalize_keys` before merging.
        let workspace = r#"{ "agentDefaults": { "maxTokens": 8192 } }"#;
        let fs = MockFs::new(Some(PathBuf::from("/tmp/clawft_test_no_home_xyz")))
            .with_file(workspace_config_path(), workspace);
        let env = MockEnv::new();
        let merged = load_config_raw(&fs, &env).await.unwrap();
        assert_eq!(
            merged
                .pointer("/agent_defaults/max_tokens")
                .and_then(Value::as_u64),
            Some(8192)
        );
    }

    // ── WEFT-10: split global / workspace layers ─────────────────────

    #[tokio::test]
    async fn load_config_layers_keeps_workspace_separate() {
        // Workspace elevates agent.chat to level 2; global has no
        // routing. After split, global must NOT contain the elevation
        // while workspace does — so PermissionResolver can clamp.
        let workspace = r#"{
            "routing": {
                "permissions": {
                    "channels": { "agent.chat": { "level": 2 } },
                    "user": { "level": 2 }
                }
            }
        }"#;
        let fs = MockFs::new(Some(PathBuf::from("/tmp/clawft_test_no_home_xyz")))
            .with_file(workspace_config_path(), workspace);
        let env = MockEnv::new();
        let layers = load_config_layers(&fs, &env).await.unwrap();

        assert!(layers.has_workspace(), "workspace overlay present");
        assert!(
            layers
                .global
                .pointer("/routing/permissions/user/level")
                .is_none(),
            "global must not contain workspace elevation"
        );
        let ws_lvl = layers
            .workspace
            .as_ref()
            .and_then(|w| w.pointer("/routing/permissions/user/level"))
            .and_then(Value::as_u64);
        assert_eq!(ws_lvl, Some(2));

        // merged() still deep-merges for non-security consumers.
        let merged = layers.merged();
        assert_eq!(
            merged
                .pointer("/routing/permissions/user/level")
                .and_then(Value::as_u64),
            Some(2)
        );
    }

    #[tokio::test]
    async fn load_config_layers_no_workspace_when_absent() {
        let fs = MockFs::new(Some(PathBuf::from("/tmp/clawft_test_no_home_xyz")));
        let env = MockEnv::new();
        let layers = load_config_layers(&fs, &env).await.unwrap();
        assert!(!layers.has_workspace());
        assert!(layers.workspace.is_none());
    }
}