tau-agent-base 0.1.0

Shared types, wire protocol, and utilities for the tau agent workspace
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
//! Three-tier config resolution: operator > project > global.
//!
//! Resolution order (highest priority first):
//!
//! 1. **Operator tier** – `~/.config/tau/projects/{name}/{filename}`
//!    (only when a project name is known)
//! 2. **Project tier** – `{project_path}/.tau/{filename}`
//!    (only when allowed — security-sensitive files like `sandbox.toml`
//!    and `plugins.toml` skip this tier)
//! 3. **Global tier** – `~/.config/tau/{filename}`
//!
//! Callers choose how to merge the results:
//!
//! - `instructions.toml` / `checklist.toml`: concatenate (operator first)
//! - `models.toml`: first-match-wins per key (operator > project > global)
//! - `sandbox.toml` / `plugins.toml`: use `load_first` with
//!   `allow_project_tier = false`

use std::path::PathBuf;

use crate::paths;

/// Build the ordered list of candidate paths for a config file.
///
/// Paths are returned in priority order (highest first).  Not all paths
/// will exist on disk — callers filter by existence.
pub fn config_paths(
    project_name: Option<&str>,
    project_path: Option<&str>,
    filename: &str,
    allow_project_tier: bool,
) -> Vec<PathBuf> {
    let mut paths = Vec::with_capacity(3);

    // 1. Operator tier
    if let Some(name) = project_name {
        paths.push(paths::project_config_dir(name).join(filename));
    }

    // 2. Project tier (skipped for security-sensitive configs)
    if allow_project_tier {
        if let Some(project) = project_path {
            paths.push(PathBuf::from(project).join(".tau").join(filename));
        }
    }

    // 3. Global tier
    paths.push(paths::config_dir().join(filename));

    paths
}

/// Load and deserialize a TOML config file from the highest-priority tier
/// where it exists.
///
/// Returns `None` when no file is found at any tier, or when all found
/// files fail to parse (warnings are printed to stderr).
pub fn load_first<T: serde::de::DeserializeOwned>(
    project_name: Option<&str>,
    project_path: Option<&str>,
    filename: &str,
    allow_project_tier: bool,
) -> Option<T> {
    for path in config_paths(project_name, project_path, filename, allow_project_tier) {
        let content = match std::fs::read_to_string(&path) {
            Ok(c) => c,
            Err(_) => continue,
        };
        match toml::from_str::<T>(&content) {
            Ok(val) => return Some(val),
            Err(e) => {
                // Use eprintln! here rather than tracing: config_chain is a
                // shared utility called from subprocesses (tasks plugin) and
                // CLI commands that don't install a tracing subscriber. In
                // the server process, stderr is captured by the parent
                // daemon.stderr.log so the warning still lands somewhere.
                eprintln!("config_chain: failed to parse {}: {}", path.display(), e);
            }
        }
    }
    None
}

/// Load and deserialize TOML config files from all tiers where they exist.
///
/// Returns a vec of `(path, parsed_value)` pairs in priority order
/// (highest first).  Files that don't exist or fail to parse are silently
/// skipped (malformed files print a warning to stderr).
///
/// Callers merge the results according to their own semantics.
pub fn load_all<T: serde::de::DeserializeOwned>(
    project_name: Option<&str>,
    project_path: Option<&str>,
    filename: &str,
    allow_project_tier: bool,
) -> Vec<(PathBuf, T)> {
    let mut results = Vec::new();
    for path in config_paths(project_name, project_path, filename, allow_project_tier) {
        let content = match std::fs::read_to_string(&path) {
            Ok(c) => c,
            Err(_) => continue,
        };
        match toml::from_str::<T>(&content) {
            Ok(val) => results.push((path, val)),
            Err(e) => {
                // See comment above about eprintln! vs tracing here.
                eprintln!("config_chain: failed to parse {}: {}", path.display(), e);
            }
        }
    }
    results
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    // Use the crate-wide env mutex to avoid races with other modules
    // (project.rs etc.) that also mutate XDG_CONFIG_HOME.

    /// Guard that overrides XDG_CONFIG_HOME and restores it on drop.
    struct XdgGuard {
        prev_xdg: Option<String>,
        prev_home: Option<String>,
    }

    impl Drop for XdgGuard {
        fn drop(&mut self) {
            match &self.prev_xdg {
                Some(v) => unsafe { std::env::set_var("XDG_CONFIG_HOME", v) },
                None => unsafe { std::env::remove_var("XDG_CONFIG_HOME") },
            }
            match &self.prev_home {
                Some(v) => unsafe { std::env::set_var("HOME", v) },
                None => unsafe { std::env::remove_var("HOME") },
            }
        }
    }

    fn set_xdg(dir: &std::path::Path) -> XdgGuard {
        let guard = XdgGuard {
            prev_xdg: std::env::var("XDG_CONFIG_HOME").ok(),
            prev_home: std::env::var("HOME").ok(),
        };
        unsafe { std::env::set_var("XDG_CONFIG_HOME", dir) };
        guard
    }

    // --- config_paths tests ---

    #[test]
    fn config_paths_all_tiers() {
        let _g = crate::TEST_ENV_MUTEX
            .lock()
            .unwrap_or_else(|p| p.into_inner());
        let config_tmp = TempDir::new().unwrap();
        let _xdg = set_xdg(config_tmp.path());

        let paths = config_paths(
            Some("myproj"),
            Some("/home/user/project"),
            "instructions.toml",
            true,
        );

        assert_eq!(paths.len(), 3);
        assert!(
            paths[0]
                .to_str()
                .unwrap()
                .contains("projects/myproj/instructions.toml")
        );
        assert!(
            paths[1]
                .to_str()
                .unwrap()
                .contains("/home/user/project/.tau/instructions.toml")
        );
        assert!(paths[2].to_str().unwrap().contains("tau/instructions.toml"));
        // Global should NOT contain "projects/"
        assert!(!paths[2].to_str().unwrap().contains("projects/"));
    }

    #[test]
    fn config_paths_skip_project_tier() {
        let _g = crate::TEST_ENV_MUTEX
            .lock()
            .unwrap_or_else(|p| p.into_inner());
        let config_tmp = TempDir::new().unwrap();
        let _xdg = set_xdg(config_tmp.path());

        let paths = config_paths(
            Some("myproj"),
            Some("/home/user/project"),
            "sandbox.toml",
            false, // security-sensitive: skip project tier
        );

        assert_eq!(paths.len(), 2);
        assert!(
            paths[0]
                .to_str()
                .unwrap()
                .contains("projects/myproj/sandbox.toml")
        );
        assert!(paths[1].to_str().unwrap().contains("tau/sandbox.toml"));
        // No project tier path
        assert!(
            paths
                .iter()
                .all(|p| !p.to_str().unwrap().contains(".tau/sandbox.toml"))
        );
    }

    #[test]
    fn config_paths_no_project_name() {
        let _g = crate::TEST_ENV_MUTEX
            .lock()
            .unwrap_or_else(|p| p.into_inner());
        let config_tmp = TempDir::new().unwrap();
        let _xdg = set_xdg(config_tmp.path());

        let paths = config_paths(None, Some("/home/user/project"), "instructions.toml", true);

        assert_eq!(paths.len(), 2);
        // No operator tier
        assert!(!paths[0].to_str().unwrap().contains("projects/"));
        assert!(
            paths[0]
                .to_str()
                .unwrap()
                .contains(".tau/instructions.toml")
        );
        assert!(paths[1].to_str().unwrap().contains("tau/instructions.toml"));
    }

    #[test]
    fn config_paths_no_project_at_all() {
        let _g = crate::TEST_ENV_MUTEX
            .lock()
            .unwrap_or_else(|p| p.into_inner());
        let config_tmp = TempDir::new().unwrap();
        let _xdg = set_xdg(config_tmp.path());

        let paths = config_paths(None, None, "models.toml", true);

        assert_eq!(paths.len(), 1);
        assert!(paths[0].to_str().unwrap().contains("tau/models.toml"));
    }

    // --- load_all tests ---

    #[derive(Debug, serde::Deserialize, PartialEq)]
    struct SimpleConfig {
        #[serde(default)]
        value: Option<String>,
    }

    #[test]
    fn load_all_returns_all_tiers() {
        let _g = crate::TEST_ENV_MUTEX
            .lock()
            .unwrap_or_else(|p| p.into_inner());

        // Set up global config
        let config_tmp = TempDir::new().unwrap();
        let _xdg = set_xdg(config_tmp.path());
        let global_dir = config_tmp.path().join("tau");
        fs::create_dir_all(&global_dir).unwrap();
        fs::write(global_dir.join("test.toml"), "value = \"global\"\n").unwrap();

        // Set up project config
        let project_tmp = TempDir::new().unwrap();
        let tau_dir = project_tmp.path().join(".tau");
        fs::create_dir_all(&tau_dir).unwrap();
        fs::write(tau_dir.join("test.toml"), "value = \"project\"\n").unwrap();

        // Set up operator config
        let operator_dir = global_dir.join("projects").join("testproj");
        fs::create_dir_all(&operator_dir).unwrap();
        fs::write(operator_dir.join("test.toml"), "value = \"operator\"\n").unwrap();

        let results: Vec<(PathBuf, SimpleConfig)> = load_all(
            Some("testproj"),
            Some(project_tmp.path().to_str().unwrap()),
            "test.toml",
            true,
        );

        assert_eq!(results.len(), 3);
        assert_eq!(results[0].1.value.as_deref(), Some("operator"));
        assert_eq!(results[1].1.value.as_deref(), Some("project"));
        assert_eq!(results[2].1.value.as_deref(), Some("global"));
    }

    #[test]
    fn load_all_skips_missing_files() {
        let _g = crate::TEST_ENV_MUTEX
            .lock()
            .unwrap_or_else(|p| p.into_inner());

        let config_tmp = TempDir::new().unwrap();
        let _xdg = set_xdg(config_tmp.path());
        let global_dir = config_tmp.path().join("tau");
        fs::create_dir_all(&global_dir).unwrap();
        fs::write(global_dir.join("test.toml"), "value = \"global\"\n").unwrap();

        // No project or operator config
        let results: Vec<(PathBuf, SimpleConfig)> = load_all(
            Some("testproj"),
            Some("/nonexistent/project"),
            "test.toml",
            true,
        );

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].1.value.as_deref(), Some("global"));
    }

    #[test]
    fn load_all_skips_malformed_toml() {
        let _g = crate::TEST_ENV_MUTEX
            .lock()
            .unwrap_or_else(|p| p.into_inner());

        let config_tmp = TempDir::new().unwrap();
        let _xdg = set_xdg(config_tmp.path());
        let global_dir = config_tmp.path().join("tau");
        fs::create_dir_all(&global_dir).unwrap();
        fs::write(global_dir.join("test.toml"), "value = \"global\"\n").unwrap();

        // Malformed project config
        let project_tmp = TempDir::new().unwrap();
        let tau_dir = project_tmp.path().join(".tau");
        fs::create_dir_all(&tau_dir).unwrap();
        fs::write(tau_dir.join("test.toml"), "this is not [[ valid toml }{").unwrap();

        let results: Vec<(PathBuf, SimpleConfig)> = load_all(
            None,
            Some(project_tmp.path().to_str().unwrap()),
            "test.toml",
            true,
        );

        // Only global should be loaded (malformed project tier is skipped)
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].1.value.as_deref(), Some("global"));
    }

    // --- load_first tests ---

    #[test]
    fn load_first_returns_highest_priority() {
        let _g = crate::TEST_ENV_MUTEX
            .lock()
            .unwrap_or_else(|p| p.into_inner());

        let config_tmp = TempDir::new().unwrap();
        let _xdg = set_xdg(config_tmp.path());
        let global_dir = config_tmp.path().join("tau");
        fs::create_dir_all(&global_dir).unwrap();
        fs::write(global_dir.join("test.toml"), "value = \"global\"\n").unwrap();

        // Operator config
        let operator_dir = global_dir.join("projects").join("testproj");
        fs::create_dir_all(&operator_dir).unwrap();
        fs::write(operator_dir.join("test.toml"), "value = \"operator\"\n").unwrap();

        let result: Option<SimpleConfig> =
            load_first(Some("testproj"), Some("/nonexistent"), "test.toml", true);

        assert_eq!(result.unwrap().value.as_deref(), Some("operator"));
    }

    #[test]
    fn load_first_falls_back_to_global() {
        let _g = crate::TEST_ENV_MUTEX
            .lock()
            .unwrap_or_else(|p| p.into_inner());

        let config_tmp = TempDir::new().unwrap();
        let _xdg = set_xdg(config_tmp.path());
        let global_dir = config_tmp.path().join("tau");
        fs::create_dir_all(&global_dir).unwrap();
        fs::write(global_dir.join("test.toml"), "value = \"global\"\n").unwrap();

        let result: Option<SimpleConfig> = load_first(None, None, "test.toml", true);

        assert_eq!(result.unwrap().value.as_deref(), Some("global"));
    }

    #[test]
    fn load_first_returns_none_when_no_file() {
        let _g = crate::TEST_ENV_MUTEX
            .lock()
            .unwrap_or_else(|p| p.into_inner());

        let config_tmp = TempDir::new().unwrap();
        let _xdg = set_xdg(config_tmp.path());
        // Don't create any files

        let result: Option<SimpleConfig> = load_first(None, None, "test.toml", true);

        assert!(result.is_none());
    }
}