Skip to content

Optionally run using user profile#1678

Merged
pakrym-oai merged 8 commits intomainfrom
pakrym/optionally-run-using-user-profile
Jul 25, 2025
Merged

Optionally run using user profile#1678
pakrym-oai merged 8 commits intomainfrom
pakrym/optionally-run-using-user-profile

Conversation

@pakrym-oai
Copy link
Copy Markdown
Collaborator

No description provided.

Copy link
Copy Markdown
Collaborator

@bolinfest bolinfest left a comment

Choose a reason for hiding this comment

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

I think this is generally a good starting point, but my biggest question is whether we should scrap ShellEnvironmentPolicy and make this new behavior the default.

Comment thread codex-rs/core/src/shell.rs Outdated

#[cfg(target_os = "macos")]
pub fn current_shell() -> Option<Shell> {
let user = whoami::username();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should probably check $SHELL first and then do this as a fallback?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

From what I gathered $SHELL is a less reliable indicator of the default shell.

Comment thread codex-rs/core/src/shell.rs
Comment thread codex-rs/core/src/codex.rs Outdated
Comment thread codex-rs/core/src/codex.rs Outdated
Comment thread codex-rs/core/src/shell.rs Outdated
Shell::Zsh(shell_path) => {
let mut result = vec![shell_path.clone(), "-c".to_string()];
if let Ok(joined) = shlex::try_join(command.iter().map(|s| s.as_str())) {
result.push(format!("source ~/.zshrc && ({joined})"));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is ~/.zshrc required to exist? Should ; be used instead of && to ignore a failure in that case?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A well-behaved ~/.zshrc should not do this, but it could write to stdout/stderr and interfere with the tool call output, no?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity, are the parens around necessary?

Though I did try the following and it still exits 42, at least:

ls && (python -c 'import sys; sys.exit(42)')

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Out of curiosity, are the parens around necessary?

in case there are more && || in the command we are trying to run

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added logic to check for zshrc

pub include_only: Vec<EnvironmentVariablePattern>,

/// If true, the shell profile will be used to run the command.
pub use_profile: bool,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Currently, I am tempted to get rid of ShellEnvironmentPolicy altogether and make the user responsible for sanitizing the environment with which they invoke codex. Or at least, make the default behavior to inherit the current environment and the user has to take action to configure something else.

Thoughts?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I don't mind it but I'd like to avoid bundling everything together.

@pakrym-oai pakrym-oai marked this pull request as ready for review July 25, 2025 18:27
Comment thread codex-rs/core/src/exec.rs
stdio_policy: StdioPolicy,
env: HashMap<String, String>,
) -> std::io::Result<Child> {
trace!(
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I left this to be able to ask for logs from users when shell exec does something strange

Comment thread codex-rs/core/Cargo.toml Outdated
uuid = { version = "1", features = ["serde", "v4"] }
wildmatch = "2.4.0"
whoami = "1.6.0"
shlex = "1.3.0"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

alpha sort?

let home = std::env::var("HOME").unwrap();
let shell_path = String::from_utf8_lossy(&shell.stdout).trim().to_string();
if shell_path.ends_with("/zsh") {
assert_eq!(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I guess it is fair to assume this test should pass on an arbitrary Mac?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, should pass (and does in ci)

Comment thread codex-rs/core/src/shell.rs Outdated
}

impl Shell {
pub fn run_with_profile(&self, command: Vec<String>) -> Option<Vec<String>> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This doesn't actually "run" anything: it just rewrites the command? Maybe create_command_shell_invocation()? I don't know, naming is hard...

@pakrym-oai pakrym-oai merged commit 7ee8712 into main Jul 25, 2025
11 checks passed
@pakrym-oai pakrym-oai deleted the pakrym/optionally-run-using-user-profile branch July 25, 2025 18:45
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants