codex-multi-workspace 0.3.3

Run Codex CLI in Docker across saved single-folder or multi-folder workspaces.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use directories::BaseDirs;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::workspace::expand_home_path;

/// Configuration key that stores the cc-switch SQLite database path.
pub const CC_SWITCH_DB: &str = "cc-switch-db";

const CONFIG_FILE_NAME: &str = "config.json";

/// A persisted user configuration entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConfigEntry {
    name: String,
    value: PathBuf,
}

impl ConfigEntry {
    /// Create a configuration entry.
    ///
    /// # Arguments
    ///
    /// * `name` - Supported configuration key.
    /// * `value` - Persisted configuration value.
    ///
    /// # Returns
    ///
    /// A configuration entry with owned fields.
    #[must_use]
    pub fn new(name: String, value: PathBuf) -> Self {
        Self { name, value }
    }

    /// Return the configuration key.
    ///
    /// # Returns
    ///
    /// The configuration key as a borrowed string slice.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Return the configuration value.
    ///
    /// # Returns
    ///
    /// The configuration value as a borrowed path.
    #[must_use]
    pub fn value(&self) -> &Path {
        &self.value
    }
}

/// Errors returned while reading or writing user configuration.
#[derive(Debug, Error)]
pub enum ConfigError {
    /// The operating system did not expose a usable home directory.
    #[error("failed to resolve user home directory")]
    MissingHomeDirectory,

    /// The requested configuration key is not supported.
    #[error("unsupported config name '{name}'; supported config names: {supported}")]
    UnsupportedConfigName {
        /// User-provided configuration key.
        name: String,
        /// Comma-separated supported configuration keys.
        supported: &'static str,
    },

    /// The configuration file could not be read or written.
    #[error("configuration file error: {0}")]
    Io(#[from] std::io::Error),

    /// The configuration file contained invalid JSON.
    #[error("configuration JSON error: {0}")]
    Json(#[from] serde_json::Error),

    /// The system clock cannot be used to create a unique temporary file name.
    #[error("system clock is before the Unix epoch")]
    InvalidSystemClock,
}

/// User-level codex-ws configuration.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct UserConfig {
    #[serde(rename = "cc-switch-db", skip_serializing_if = "Option::is_none")]
    cc_switch_db: Option<PathBuf>,
}

impl UserConfig {
    /// Return the configured cc-switch database path.
    ///
    /// # Returns
    ///
    /// The configured path, if the user set `cc-switch-db`.
    #[must_use]
    pub fn cc_switch_db(&self) -> Option<&Path> {
        self.cc_switch_db.as_deref()
    }

    /// Set a supported configuration value.
    ///
    /// # Arguments
    ///
    /// * `name` - Supported configuration key.
    /// * `value` - Value to store for the key.
    ///
    /// # Errors
    ///
    /// Returns [`ConfigError::UnsupportedConfigName`] when `name` is not supported.
    pub fn set_value(&mut self, name: &str, value: PathBuf) -> Result<(), ConfigError> {
        match parse_config_name(name)? {
            ConfigName::CcSwitchDbRoute => {
                self.cc_switch_db = Some(value);
                Ok(())
            }
        }
    }

    /// Return one supported configuration value.
    ///
    /// # Arguments
    ///
    /// * `name` - Supported configuration key.
    ///
    /// # Returns
    ///
    /// The configuration entry when the key is set.
    ///
    /// # Errors
    ///
    /// Returns [`ConfigError::UnsupportedConfigName`] when `name` is not supported.
    pub fn get_value(&self, name: &str) -> Result<Option<ConfigEntry>, ConfigError> {
        match parse_config_name(name)? {
            ConfigName::CcSwitchDbRoute => Ok(self
                .cc_switch_db
                .as_ref()
                .map(|value| ConfigEntry::new(CC_SWITCH_DB.to_owned(), value.clone()))),
        }
    }

    /// Return all configured values.
    ///
    /// # Returns
    ///
    /// Configured entries in stable key order.
    #[must_use]
    pub fn entries(&self) -> Vec<ConfigEntry> {
        self.cc_switch_db
            .as_ref()
            .map(|value| ConfigEntry::new(CC_SWITCH_DB.to_owned(), value.clone()))
            .into_iter()
            .collect()
    }
}

/// Return the default codex-ws state root.
///
/// # Returns
///
/// The `.codex-ws` directory under the real user home directory.
///
/// # Errors
///
/// Returns [`ConfigError::MissingHomeDirectory`] when the OS does not expose a home directory.
pub fn default_state_root() -> Result<PathBuf, ConfigError> {
    Ok(home_dir()?.join(".codex-ws"))
}

/// Return the default user configuration directory.
///
/// # Returns
///
/// The `config` directory under the codex-ws state root.
///
/// # Errors
///
/// Returns [`ConfigError::MissingHomeDirectory`] when the OS does not expose a home directory.
pub fn default_config_dir() -> Result<PathBuf, ConfigError> {
    Ok(default_state_root()?.join("config"))
}

/// Return the default user configuration file path.
///
/// # Returns
///
/// The `config.json` path under the codex-ws config directory.
///
/// # Errors
///
/// Returns [`ConfigError::MissingHomeDirectory`] when the OS does not expose a home directory.
pub fn default_config_file_path() -> Result<PathBuf, ConfigError> {
    Ok(default_config_dir()?.join(CONFIG_FILE_NAME))
}

/// Return the fallback cc-switch database path.
///
/// # Returns
///
/// The legacy cc-switch database path under the user's home directory.
///
/// # Errors
///
/// Returns [`ConfigError::MissingHomeDirectory`] when the home directory cannot be resolved.
pub fn default_cc_switch_database_path() -> Result<PathBuf, ConfigError> {
    let default_path = home_dir()?.join(".cc-switch").join("cc-switch.db");
    #[cfg(windows)]
    {
        if !default_path.exists() {
            if let Ok(home_env) = std::env::var("HOME") {
                let trimmed = home_env.trim();
                if !trimmed.is_empty() {
                    let legacy_path = PathBuf::from(trimmed)
                        .join(".cc-switch")
                        .join("cc-switch.db");
                    if legacy_path.exists() {
                        return Ok(legacy_path);
                    }
                }
            }
        }
    }
    Ok(default_path)
}

/// Load user configuration from the default codex-ws config file.
///
/// # Returns
///
/// Parsed user configuration, or an empty configuration when the file does not exist.
///
/// # Errors
///
/// Returns an error when the config path cannot be resolved, the file cannot be read, or its JSON
/// is invalid.
pub fn load_default_user_config() -> Result<UserConfig, ConfigError> {
    load_user_config(&default_config_file_path()?)
}

/// Load user configuration from a file path.
///
/// # Arguments
///
/// * `path` - Configuration JSON path.
///
/// # Returns
///
/// Parsed user configuration, or an empty configuration when the file does not exist.
///
/// # Errors
///
/// Returns an error when the file cannot be read or its JSON is invalid.
pub fn load_user_config(path: &Path) -> Result<UserConfig, ConfigError> {
    if !path.exists() {
        return Ok(UserConfig::default());
    }

    let content = fs::read_to_string(path)?;
    Ok(serde_json::from_str(&content)?)
}

/// Save user configuration to a file path.
///
/// # Arguments
///
/// * `path` - Configuration JSON path.
/// * `config` - User configuration to persist.
///
/// # Errors
///
/// Returns an error when the parent directory cannot be created or the file cannot be written.
pub fn save_user_config(path: &Path, config: &UserConfig) -> Result<(), ConfigError> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }

    let content = serde_json::to_string_pretty(config)?;
    atomic_write(path, content.as_bytes())?;
    Ok(())
}

/// Set a configuration value in the default codex-ws config file.
///
/// # Arguments
///
/// * `name` - Supported configuration key.
/// * `value` - Value to persist.
///
/// # Errors
///
/// Returns an error when the key is unsupported or the config file cannot be updated.
pub fn set_default_config_value(name: &str, value: PathBuf) -> Result<PathBuf, ConfigError> {
    let path = default_config_file_path()?;
    let mut config = load_user_config(&path)?;
    config.set_value(name, expand_home_path(value))?;
    save_user_config(&path, &config)?;
    Ok(path)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ConfigName {
    CcSwitchDbRoute,
}

fn parse_config_name(name: &str) -> Result<ConfigName, ConfigError> {
    match name {
        CC_SWITCH_DB => Ok(ConfigName::CcSwitchDbRoute),
        _ => Err(ConfigError::UnsupportedConfigName {
            name: name.to_owned(),
            supported: CC_SWITCH_DB,
        }),
    }
}

fn home_dir() -> Result<PathBuf, ConfigError> {
    BaseDirs::new()
        .map(|dirs| dirs.home_dir().to_path_buf())
        .ok_or(ConfigError::MissingHomeDirectory)
}

fn atomic_write(path: &Path, content: &[u8]) -> Result<(), ConfigError> {
    let parent = path.parent().ok_or_else(|| {
        std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!("invalid configuration path '{}'", path.display()),
        )
    })?;
    let file_name = path.file_name().ok_or_else(|| {
        std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!("invalid configuration file name '{}'", path.display()),
        )
    })?;
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|_| ConfigError::InvalidSystemClock)?
        .as_nanos();
    let temporary_path = parent.join(format!(
        "{}.tmp.{}-{timestamp}",
        file_name.to_string_lossy(),
        std::process::id()
    ));

    fs::write(&temporary_path, content)?;
    #[cfg(windows)]
    {
        if path.exists() {
            fs::remove_file(path)?;
        }
    }
    fs::rename(temporary_path, path)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::{SystemTime, UNIX_EPOCH};

    use super::*;

    static TEMP_DIR_COUNTER: AtomicUsize = AtomicUsize::new(0);

    #[test]
    fn user_config_sets_and_gets_cc_switch_database_path() {
        let mut config = UserConfig::default();

        config
            .set_value(CC_SWITCH_DB, PathBuf::from("/tmp/cc-switch.db"))
            .expect("supported config should set");

        let entry = config
            .get_value(CC_SWITCH_DB)
            .expect("supported config should get")
            .expect("entry should be present");
        assert_eq!(entry.name(), CC_SWITCH_DB);
        assert_eq!(entry.value(), Path::new("/tmp/cc-switch.db"));
    }

    #[test]
    fn user_config_rejects_unsupported_config_names() {
        let mut config = UserConfig::default();
        let error = config
            .set_value("unknown", PathBuf::from("value"))
            .expect_err("unsupported config should fail")
            .to_string();

        assert_eq!(
            error,
            "unsupported config name 'unknown'; supported config names: cc-switch-db"
        );
    }

    #[test]
    fn load_user_config_returns_default_when_file_is_missing() {
        let temp_dir = TestTempDir::create();
        let config = load_user_config(&temp_dir.path().join("missing.json"))
            .expect("missing config should load as default");

        assert_eq!(config, UserConfig::default());
    }

    #[test]
    fn save_user_config_creates_parent_directories() {
        let temp_dir = TestTempDir::create();
        let config_path = temp_dir.path().join("nested").join("config.json");
        let mut config = UserConfig::default();
        config
            .set_value(CC_SWITCH_DB, PathBuf::from("/tmp/cc-switch.db"))
            .expect("supported config should set");

        save_user_config(&config_path, &config).expect("config should save");
        let loaded = load_user_config(&config_path).expect("config should load");

        assert_eq!(loaded, config);
    }

    #[derive(Debug)]
    struct TestTempDir {
        path: PathBuf,
    }

    impl TestTempDir {
        fn create() -> Self {
            let counter = TEMP_DIR_COUNTER.fetch_add(1, Ordering::Relaxed);
            let timestamp = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("system clock should be after Unix epoch")
                .as_nanos();
            let path = std::env::temp_dir().join(format!(
                "codex-ws-config-test-{}-{timestamp}-{counter}",
                std::process::id()
            ));
            fs::create_dir(&path).expect("temporary test directory should be created");
            Self { path }
        }

        fn path(&self) -> &Path {
            &self.path
        }
    }

    impl Drop for TestTempDir {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.path);
        }
    }
}