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
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
use thiserror::Error;

/// Environment variable used by the runtime entrypoint for apt packages.
pub const CODEX_WS_APT_PACKAGES_ENV: &str = "CODEX_WS_APT_PACKAGES";

/// Environment variable used by the runtime entrypoint for setup commands.
pub const CODEX_WS_SETUP_COMMANDS_ENV: &str = "CODEX_WS_SETUP_COMMANDS";

/// Environment variable used by the runtime entrypoint for Python version selection.
pub const CODEX_WS_PYTHON_VERSION_ENV: &str = "CODEX_WS_PYTHON_VERSION";

/// Environment variable used by the runtime entrypoint for Node.js version selection.
pub const CODEX_WS_NODE_VERSION_ENV: &str = "CODEX_WS_NODE_VERSION";

/// Environment variable used by the runtime entrypoint for Go version selection.
pub const CODEX_WS_GO_VERSION_ENV: &str = "CODEX_WS_GO_VERSION";

/// Environment variable used by the runtime entrypoint for Rust version selection.
pub const CODEX_WS_RUST_VERSION_ENV: &str = "CODEX_WS_RUST_VERSION";

/// Environment variable used by the runtime entrypoint for Java version selection.
pub const CODEX_WS_JAVA_VERSION_ENV: &str = "CODEX_WS_JAVA_VERSION";

/// Environment variable used by the runtime entrypoint for Clang version selection.
pub const CODEX_WS_CLANG_VERSION_ENV: &str = "CODEX_WS_CLANG_VERSION";

/// Environment variable used by the runtime entrypoint for C compiler version selection.
pub const CODEX_WS_C_VERSION_ENV: &str = "CODEX_WS_C_VERSION";

/// Environment variable used by the runtime entrypoint for C++ compiler version selection.
pub const CODEX_WS_CPP_VERSION_ENV: &str = "CODEX_WS_CPP_VERSION";

/// Environment variable used by the runtime entrypoint for Ruby version selection.
pub const CODEX_WS_RUBY_VERSION_ENV: &str = "CODEX_WS_RUBY_VERSION";

/// Environment variable used by the runtime entrypoint for PHP version selection.
pub const CODEX_WS_PHP_VERSION_ENV: &str = "CODEX_WS_PHP_VERSION";

/// Environment variable used by the runtime entrypoint for Deno version selection.
pub const CODEX_WS_DENO_VERSION_ENV: &str = "CODEX_WS_DENO_VERSION";

/// Environment variable used by the runtime entrypoint for Bun version selection.
pub const CODEX_WS_BUN_VERSION_ENV: &str = "CODEX_WS_BUN_VERSION";

/// Environment variable used by the runtime entrypoint for Zig version selection.
pub const CODEX_WS_ZIG_VERSION_ENV: &str = "CODEX_WS_ZIG_VERSION";

/// Environment variable used by the runtime entrypoint for .NET version selection.
pub const CODEX_WS_DOTNET_VERSION_ENV: &str = "CODEX_WS_DOTNET_VERSION";

/// Docker environment variable generated from a workspace runtime config.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RuntimeEnvironmentVariable {
    name: &'static str,
    value: String,
}

impl RuntimeEnvironmentVariable {
    /// Create a runtime environment variable.
    ///
    /// # Arguments
    ///
    /// * `name` - Environment variable name recognized by the runtime entrypoint.
    /// * `value` - Environment variable value passed to Docker.
    ///
    /// # Returns
    ///
    /// A runtime environment variable.
    #[must_use]
    pub fn new(name: &'static str, value: String) -> Self {
        Self { name, value }
    }

    /// Return the environment variable name.
    ///
    /// # Returns
    ///
    /// The runtime entrypoint environment variable name.
    #[must_use]
    pub const fn name(&self) -> &'static str {
        self.name
    }

    /// Return the environment variable value.
    ///
    /// # Returns
    ///
    /// The value passed to Docker.
    #[must_use]
    pub fn value(&self) -> &str {
        &self.value
    }

    /// Return the `NAME=value` form accepted by `docker run -e`.
    ///
    /// # Returns
    ///
    /// A Docker environment assignment.
    #[must_use]
    pub fn docker_assignment(&self) -> String {
        format!("{}={}", self.name, self.value)
    }
}

/// Validate apt package names from a workspace manifest.
///
/// # Arguments
///
/// * `packages` - Package names requested by `runtime.apt`.
///
/// # Returns
///
/// Trimmed package names in input order.
///
/// # Errors
///
/// Returns [`RuntimeSpecError::EmptyAptPackage`] for a blank package or
/// [`RuntimeSpecError::InvalidAptPackage`] when a package contains shell metacharacters.
pub fn validate_apt_packages(packages: Vec<String>) -> Result<Vec<String>, RuntimeSpecError> {
    let mut validated_packages = Vec::with_capacity(packages.len());
    for package in packages {
        let package = package.trim().to_owned();
        if package.is_empty() {
            return Err(RuntimeSpecError::EmptyAptPackage);
        }
        if !is_valid_apt_package(&package) {
            return Err(RuntimeSpecError::InvalidAptPackage { package });
        }
        validated_packages.push(package);
    }

    Ok(validated_packages)
}

/// Validate setup commands from a workspace manifest.
///
/// # Arguments
///
/// * `commands` - Shell commands requested by `runtime.setup`.
///
/// # Returns
///
/// Trimmed commands in input order.
///
/// # Errors
///
/// Returns [`RuntimeSpecError::EmptySetupCommand`] when a setup command is blank.
pub fn validate_setup_commands(commands: Vec<String>) -> Result<Vec<String>, RuntimeSpecError> {
    let mut validated_commands = Vec::with_capacity(commands.len());
    for command in commands {
        let command = command.trim().to_owned();
        if command.is_empty() {
            return Err(RuntimeSpecError::EmptySetupCommand);
        }
        validated_commands.push(command);
    }

    Ok(validated_commands)
}

/// Validate a runtime tool version from a workspace manifest.
///
/// # Arguments
///
/// * `tool` - Tool name used in diagnostics.
/// * `version` - Optional version requested by the workspace manifest.
///
/// # Returns
///
/// A trimmed version when one was configured.
///
/// # Errors
///
/// Returns [`RuntimeSpecError::EmptyToolVersion`] for a blank version or
/// [`RuntimeSpecError::InvalidToolVersion`] when the version contains shell metacharacters.
pub fn validate_tool_version(
    tool: RuntimeTool,
    version: Option<String>,
) -> Result<Option<RuntimeToolVersion>, RuntimeSpecError> {
    let Some(version) = version else {
        return Ok(None);
    };
    let version = version.trim().to_owned();
    if version.is_empty() {
        return Err(RuntimeSpecError::EmptyToolVersion { tool });
    }
    if !is_valid_tool_version(&version) {
        return Err(RuntimeSpecError::InvalidToolVersion { tool, version });
    }

    Ok(Some(RuntimeToolVersion::new(tool, version)))
}

/// Validate a set of requested runtime tool versions.
///
/// # Arguments
///
/// * `versions` - Runtime tool versions collected from a workspace manifest.
///
/// # Returns
///
/// Runtime tool versions in input order.
///
/// # Errors
///
/// Returns [`RuntimeSpecError::ConflictingCompilerVersions`] when `c`, `cpp`, and `clang`
/// request different LLVM Clang versions.
pub fn validate_runtime_tool_versions(
    versions: Vec<RuntimeToolVersion>,
) -> Result<Vec<RuntimeToolVersion>, RuntimeSpecError> {
    let mut clang_version: Option<&str> = None;

    for version in &versions {
        if !version.tool().uses_clang() {
            continue;
        }
        if let Some(existing_version) = clang_version
            && existing_version != version.version()
        {
            return Err(RuntimeSpecError::ConflictingCompilerVersions {
                first: existing_version.to_owned(),
                second: version.version().to_owned(),
            });
        }
        clang_version = Some(version.version());
    }

    Ok(versions)
}

fn is_valid_apt_package(package: &str) -> bool {
    package.bytes().all(|byte| {
        byte.is_ascii_alphanumeric()
            || matches!(byte, b'+' | b'-' | b'.' | b'_' | b':' | b'=' | b'~')
    })
}

fn is_valid_tool_version(version: &str) -> bool {
    version.bytes().all(|byte| {
        byte.is_ascii_alphanumeric()
            || matches!(byte, b'+' | b'-' | b'.' | b'_' | b':' | b'/' | b'@')
    })
}

/// Runtime tools that can be installed declaratively.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeTool {
    /// Python installed with uv.
    Python,
    /// Node.js installed with mise.
    Node,
    /// Go installed with mise.
    Go,
    /// Rust installed with mise.
    Rust,
    /// Java installed with mise.
    Java,
    /// Clang installed with LLVM apt packages.
    Clang,
    /// C compiler installed with LLVM apt packages.
    C,
    /// C++ compiler installed with LLVM apt packages.
    Cpp,
    /// Ruby installed with mise.
    Ruby,
    /// PHP installed with mise.
    Php,
    /// Deno installed with mise.
    Deno,
    /// Bun installed with mise.
    Bun,
    /// Zig installed with mise.
    Zig,
    /// .NET SDK installed with mise.
    Dotnet,
}

impl RuntimeTool {
    /// Return the manifest field name for this tool.
    ///
    /// # Returns
    ///
    /// Lowercase tool name used in workspace manifests.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Python => "python",
            Self::Node => "node",
            Self::Go => "go",
            Self::Rust => "rust",
            Self::Java => "java",
            Self::Clang => "clang",
            Self::C => "c",
            Self::Cpp => "cpp",
            Self::Ruby => "ruby",
            Self::Php => "php",
            Self::Deno => "deno",
            Self::Bun => "bun",
            Self::Zig => "zig",
            Self::Dotnet => "dotnet",
        }
    }

    /// Return the environment variable consumed by the runtime entrypoint.
    ///
    /// # Returns
    ///
    /// Environment variable name for this declarative runtime tool.
    #[must_use]
    pub const fn environment_variable(self) -> &'static str {
        match self {
            Self::Python => CODEX_WS_PYTHON_VERSION_ENV,
            Self::Node => CODEX_WS_NODE_VERSION_ENV,
            Self::Go => CODEX_WS_GO_VERSION_ENV,
            Self::Rust => CODEX_WS_RUST_VERSION_ENV,
            Self::Java => CODEX_WS_JAVA_VERSION_ENV,
            Self::Clang => CODEX_WS_CLANG_VERSION_ENV,
            Self::C => CODEX_WS_C_VERSION_ENV,
            Self::Cpp => CODEX_WS_CPP_VERSION_ENV,
            Self::Ruby => CODEX_WS_RUBY_VERSION_ENV,
            Self::Php => CODEX_WS_PHP_VERSION_ENV,
            Self::Deno => CODEX_WS_DENO_VERSION_ENV,
            Self::Bun => CODEX_WS_BUN_VERSION_ENV,
            Self::Zig => CODEX_WS_ZIG_VERSION_ENV,
            Self::Dotnet => CODEX_WS_DOTNET_VERSION_ENV,
        }
    }

    const fn uses_clang(self) -> bool {
        matches!(self, Self::Clang | Self::C | Self::Cpp)
    }
}

/// One declarative runtime tool version selected for a workspace.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RuntimeToolVersion {
    tool: RuntimeTool,
    version: String,
}

impl RuntimeToolVersion {
    /// Create a runtime tool version.
    ///
    /// # Arguments
    ///
    /// * `tool` - Runtime tool requested by the workspace.
    /// * `version` - Version string passed to the runtime installer.
    ///
    /// # Returns
    ///
    /// A runtime tool version.
    #[must_use]
    pub fn new(tool: RuntimeTool, version: String) -> Self {
        Self { tool, version }
    }

    /// Return the requested runtime tool.
    ///
    /// # Returns
    ///
    /// Runtime tool enum value.
    #[must_use]
    pub const fn tool(&self) -> RuntimeTool {
        self.tool
    }

    /// Return the requested tool version.
    ///
    /// # Returns
    ///
    /// Version string passed to the runtime installer.
    #[must_use]
    pub fn version(&self) -> &str {
        &self.version
    }

    /// Convert this runtime tool into a Docker environment variable.
    ///
    /// # Returns
    ///
    /// Runtime entrypoint environment variable.
    #[must_use]
    pub fn environment_variable(&self) -> RuntimeEnvironmentVariable {
        RuntimeEnvironmentVariable::new(self.tool.environment_variable(), self.version.clone())
    }
}

/// Errors returned while parsing workspace runtime setup.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum RuntimeSpecError {
    /// An apt package entry was empty or only whitespace.
    #[error("runtime apt package cannot be empty")]
    EmptyAptPackage,

    /// An apt package entry contained characters that are unsafe for shell word splitting.
    #[error("invalid runtime apt package '{package}'")]
    InvalidAptPackage {
        /// Invalid package entry.
        package: String,
    },

    /// A setup command was empty or only whitespace.
    #[error("runtime setup command cannot be empty")]
    EmptySetupCommand,

    /// A declarative runtime tool version was empty or only whitespace.
    #[error("runtime {tool} version cannot be empty", tool = .tool.name())]
    EmptyToolVersion {
        /// Tool with an empty version.
        tool: RuntimeTool,
    },

    /// A declarative runtime tool version contained unsafe characters.
    #[error("invalid runtime {tool} version '{version}'", tool = .tool.name())]
    InvalidToolVersion {
        /// Tool with an invalid version.
        tool: RuntimeTool,
        /// Invalid version text.
        version: String,
    },

    /// Multiple C/C++ compiler aliases requested different Clang versions.
    #[error("conflicting C/C++ runtime versions '{first}' and '{second}'")]
    ConflictingCompilerVersions {
        /// First configured compiler version.
        first: String,
        /// Conflicting compiler version.
        second: String,
    },
}

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

    #[test]
    fn validate_apt_packages_accepts_common_package_syntax() {
        let packages = validate_apt_packages(vec![
            " python3 ".to_owned(),
            "libssl-dev:amd64".to_owned(),
            "nodejs=22.0.0-1nodesource1".to_owned(),
        ])
        .expect("apt packages should validate");

        assert_eq!(
            packages,
            vec![
                "python3".to_owned(),
                "libssl-dev:amd64".to_owned(),
                "nodejs=22.0.0-1nodesource1".to_owned()
            ]
        );
    }

    #[test]
    fn validate_apt_packages_rejects_shell_metacharacters() {
        let error = validate_apt_packages(vec!["python3;curl".to_owned()])
            .expect_err("shell metacharacters should fail");

        assert!(matches!(
            error,
            RuntimeSpecError::InvalidAptPackage { package } if package == "python3;curl"
        ));
    }

    #[test]
    fn validate_setup_commands_rejects_empty_commands() {
        let error =
            validate_setup_commands(vec![" ".to_owned()]).expect_err("blank command should fail");

        assert_eq!(error, RuntimeSpecError::EmptySetupCommand);
    }

    #[test]
    fn validate_tool_version_trims_versions() {
        let version = validate_tool_version(RuntimeTool::Python, Some(" 3.13 ".to_owned()))
            .expect("version should validate");

        assert_eq!(
            version,
            Some(RuntimeToolVersion::new(
                RuntimeTool::Python,
                "3.13".to_owned()
            ))
        );
    }

    #[test]
    fn validate_tool_version_rejects_shell_metacharacters() {
        let error = validate_tool_version(RuntimeTool::Go, Some("1.24;curl".to_owned()))
            .expect_err("shell metacharacters should fail");

        assert!(matches!(
            error,
            RuntimeSpecError::InvalidToolVersion {
                tool: RuntimeTool::Go,
                version
            } if version == "1.24;curl"
        ));
    }

    #[test]
    fn validate_runtime_tool_versions_accepts_matching_compiler_aliases() {
        let versions = validate_runtime_tool_versions(vec![
            RuntimeToolVersion::new(RuntimeTool::C, "20".to_owned()),
            RuntimeToolVersion::new(RuntimeTool::Cpp, "20".to_owned()),
        ])
        .expect("matching compiler aliases should validate");

        assert_eq!(versions.len(), 2);
    }

    #[test]
    fn validate_runtime_tool_versions_rejects_conflicting_compiler_aliases() {
        let error = validate_runtime_tool_versions(vec![
            RuntimeToolVersion::new(RuntimeTool::C, "20".to_owned()),
            RuntimeToolVersion::new(RuntimeTool::Cpp, "21".to_owned()),
        ])
        .expect_err("conflicting compiler aliases should fail");

        assert!(matches!(
            error,
            RuntimeSpecError::ConflictingCompilerVersions { first, second }
                if first == "20" && second == "21"
        ));
    }
}