Skip to content

[codex] add flmexec runtime parameter#473

Merged
k82cn merged 2 commits into
xflops:mainfrom
k82cn:codex/flmexec-runtime-parameter
May 27, 2026
Merged

[codex] add flmexec runtime parameter#473
k82cn merged 2 commits into
xflops:mainfrom
k82cn:codex/flmexec-runtime-parameter

Conversation

@k82cn

@k82cn k82cn commented May 27, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add an optional runtime field to flmexec Script requests and the flmexec CLI
  • use runtime as the Python version for python scripts, defaulting to 3.12
  • use runtime as the shell type for shell scripts, defaulting to bash
  • update flmexec examples and the e2e helper to carry the optional runtime field

Verification

  • cargo fmt --check -p flmexec
  • cargo test -p flmexec
  • cargo check -p flmexec
  • uv run --extra dev ruff check tests/test_flmexec.py (from e2e/)
  • python3 -m py_compile e2e/tests/test_flmexec.py examples/agents/sra/apis.py examples/agents/scripts/main.py

@k82cn k82cn marked this pull request as ready for review May 27, 2026 00:38

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a runtime parameter to script execution, allowing users to specify a particular Python version or shell type (e.g., zsh, bash). While this adds valuable flexibility, the changes in the Python script runner introduce a regression by removing the fallback chain that auto-detects the latest installed Python version or reads from the FLAME_PYTHON_VERSION_ENV environment variable. Feedback focuses on restoring this fallback behavior to preserve backward compatibility, updating tests accordingly, and optimizing the shell path validation to avoid unnecessary heap allocations.

Comment thread flmexec/src/script/lang/python.rs Outdated
.or_insert_with(|| flame_home.to_string_lossy().to_string());

let python_version = python_version(envs, &flame_home);
let python_version = python_version(runtime);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Pass envs and &flame_home to python_version to allow falling back to the propagated FLAME_PYTHON_VERSION_ENV environment variable and auto-detecting the latest installed Python version when runtime is not specified.

Suggested change
let python_version = python_version(runtime);
let python_version = python_version(envs, &flame_home, runtime);

Comment thread flmexec/src/script/lang/python.rs Outdated
Comment on lines 101 to 112
fn python_version(runtime: Option<&str>) -> String {
runtime
.map(str::trim)
.filter(|version| !version.is_empty())
.map(version_number)
.map(ToOwned::to_owned)
.or_else(|| latest_installed_python_version(flame_home))
.unwrap_or_else(|| DEFAULT_PYTHON_VERSION.to_string())
}

fn version_number(version: &str) -> &str {
version.strip_prefix("python").unwrap_or(version)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

By only checking runtime and defaulting to DEFAULT_PYTHON_VERSION, the new implementation completely ignores the propagated FLAME_PYTHON_VERSION_ENV environment variable and removes the latest_installed_python_version auto-detection fallback. This is a regression for existing environments that rely on these fallbacks.

Restoring the full fallback chain (checking runtime first, then FLAME_PYTHON_VERSION_ENV, then auto-detecting the latest installed version, and finally defaulting to DEFAULT_PYTHON_VERSION) preserves backward compatibility and robust environment-based configuration.

fn python_version(envs: &HashMap<String, String>, flame_home: &Path, runtime: Option<&str>) -> String {
    runtime
        .map(str::trim)
        .filter(|version| !version.is_empty())
        .map(version_number)
        .map(ToOwned::to_owned)
        .or_else(|| {
            envs.get(FLAME_PYTHON_VERSION_ENV)
                .map(|version| version_number(version))
                .filter(|version| !version.is_empty())
                .map(ToOwned::to_owned)
        })
        .or_else(|| latest_installed_python_version(flame_home))
        .unwrap_or_else(|| DEFAULT_PYTHON_VERSION.to_string())
}

fn version_number(version: &str) -> &str {
    version.strip_prefix("python").unwrap_or(version)
}

fn latest_installed_python_version(flame_home: &Path) -> Option<String> {
    let lib_path = flame_home.join("lib");
    let mut versions = fs::read_dir(lib_path)
        .ok()?
        .flatten()
        .filter_map(|entry| {
            let path = entry.path();
            if !path.is_dir() || !path.join("site-packages").is_dir() {
                return None;
            }

            let name = entry.file_name();
            let name = name.to_string_lossy();
            name.strip_prefix("python")
                .filter(|version| !version.is_empty())
                .map(|version| version.to_string())
        })
        .collect::<Vec<_>>();

    versions.sort_by_key(|version| minor_version(version));
    versions.pop()
}

fn minor_version(version: &str) -> Vec<u32> {
    version
        .split('.')
        .map(|part| part.parse::<u32>().unwrap_or(0))
        .collect()
}

Comment thread flmexec/src/script/lang/python.rs Outdated
Comment on lines 290 to 294
(
FLAME_PYTHON_VERSION_ENV.to_string(),
"python3.11".to_string(),
),
]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since we restored the fallback to FLAME_PYTHON_VERSION_ENV, we should remove it from this test's environment setup so that the test correctly verifies falling back to DEFAULT_PYTHON_VERSION when no version is specified anywhere.

        ]);

Comment thread flmexec/src/script/lang/shell.rs Outdated
Comment on lines +82 to +84
let supported_path = SUPPORTED_SHELLS
.iter()
.any(|shell| runtime == format!("/bin/{shell}") || runtime == format!("/usr/bin/{shell}"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Optimize the supported_path check to avoid heap allocations from format! in the loop by using strip_prefix.

    let supported_path = SUPPORTED_SHELLS.iter().any(|&shell| {
        runtime.strip_prefix("/bin/").map_or(false, |s| s == shell)
            || runtime.strip_prefix("/usr/bin/").map_or(false, |s| s == shell)
    });

@k82cn k82cn merged commit 57da9d9 into xflops:main May 27, 2026
6 checks passed
@k82cn k82cn deleted the codex/flmexec-runtime-parameter branch May 27, 2026 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant