Compare commits

...

1 Commits

Author SHA1 Message Date
Abhishek Bhardwaj
2f0bc514b7 feature: Add "!cmd" user shell execution
- protocol: add Op::RunUserShellCommand to model a user-initiated one-off command
- core: handle new Op by spawning a cancellable task that runs the command using the user’s default shell; stream output via ExecCommand* events; send TaskStarted/TaskComplete; track as current_task so Interrupt works
- tui: detect leading '!' in composer submission and dispatch Op::RunUserShellCommand instead of sending a user message

No changes to sandbox env var behavior; uses existing exec pipeline and event types.
2025-09-24 18:28:23 -07:00
10 changed files with 352 additions and 17 deletions

View File

@@ -135,6 +135,7 @@ use codex_protocol::models::ResponseInputItem;
use codex_protocol::models::ResponseItem;
use codex_protocol::models::ShellToolCallParams;
use codex_protocol::protocol::InitialHistory;
use uuid::Uuid;
pub mod compact;
use self::compact::build_compacted_history;
@@ -812,6 +813,7 @@ impl Session {
command_for_display,
cwd,
apply_patch,
is_user_shell_command,
} = exec_command_context;
let msg = match apply_patch {
Some(ApplyPatchCommandContext {
@@ -834,6 +836,7 @@ impl Session {
.into_iter()
.map(Into::into)
.collect(),
is_user_shell_command,
}),
};
let event = Event {
@@ -1063,6 +1066,7 @@ pub(crate) struct ExecCommandContext {
pub(crate) command_for_display: Vec<String>,
pub(crate) cwd: PathBuf,
pub(crate) apply_patch: Option<ApplyPatchCommandContext>,
pub(crate) is_user_shell_command: bool,
}
#[derive(Clone, Debug)]
@@ -1518,6 +1522,9 @@ async fn submission_loop(
};
sess.send_event(event).await;
}
Op::RunUserShellCommand { command } => {
spawn_user_shell_command_task(sess.clone(), &turn_context, sub.id, command).await;
}
Op::Review { review_request } => {
spawn_review_thread(
sess.clone(),
@@ -1536,6 +1543,101 @@ async fn submission_loop(
debug!("Agent loop exited");
}
async fn spawn_user_shell_command_task(
sess: Arc<Session>,
turn_context: &Arc<TurnContext>,
sub_id: String,
command: String,
) {
let handle = {
let sess = sess.clone();
let turn_context = turn_context.clone();
let spawn_sub_id = sub_id.clone();
tokio::spawn(async move {
run_user_shell_command(sess, turn_context, spawn_sub_id, command).await;
})
.abort_handle()
};
sess.set_task(AgentTask {
sess: sess.clone(),
sub_id,
handle,
kind: AgentTaskKind::Regular,
})
.await;
}
async fn run_user_shell_command(
sess: Arc<Session>,
turn_context: Arc<TurnContext>,
sub_id: String,
command: String,
) {
let event = Event {
id: sub_id.clone(),
msg: EventMsg::TaskStarted(TaskStartedEvent {
model_context_window: turn_context.client.get_model_context_window(),
}),
};
sess.send_event(event).await;
let shell_invocation = sess
.user_shell
.format_user_shell_script(&command)
.unwrap_or_else(|| vec![command.clone()]);
let params = ExecParams {
command: shell_invocation.clone(),
cwd: turn_context.cwd.clone(),
timeout_ms: None,
env: create_env(&turn_context.shell_environment_policy),
with_escalated_permissions: None,
justification: None,
};
let mut turn_diff_tracker = TurnDiffTracker::new();
let call_id = Uuid::new_v4().to_string();
let exec_ctx = ExecCommandContext {
sub_id: sub_id.clone(),
call_id: call_id.clone(),
command_for_display: shell_invocation,
cwd: params.cwd.clone(),
apply_patch: None,
is_user_shell_command: true,
};
let sandbox_policy = SandboxPolicy::DangerFullAccess;
let _ = sess
.run_exec_with_events(
&mut turn_diff_tracker,
exec_ctx,
ExecInvokeArgs {
params,
sandbox_type: SandboxType::None,
sandbox_policy: &sandbox_policy,
sandbox_cwd: &turn_context.cwd,
codex_linux_sandbox_exe: &sess.codex_linux_sandbox_exe,
stdout_stream: Some(StdoutStream {
sub_id: sub_id.clone(),
call_id: call_id.clone(),
tx_event: sess.tx_event.clone(),
}),
},
)
.await;
let complete = Event {
id: sub_id,
msg: EventMsg::TaskComplete(TaskCompleteEvent {
last_agent_message: None,
}),
};
sess.send_event(complete).await;
}
/// Spawn a review thread using the given prompt.
async fn spawn_review_thread(
sess: Arc<Session>,
@@ -2800,6 +2902,7 @@ async fn handle_container_exec_with_params(
changes: convert_apply_patch_to_protocol(&action),
},
),
is_user_shell_command: false,
};
let params = maybe_translate_shell_command(params, sess, turn_context);

View File

@@ -30,6 +30,28 @@ pub enum Shell {
}
impl Shell {
pub fn format_user_shell_script(&self, script: &str) -> Option<Vec<String>> {
match self {
Shell::Zsh(zsh) => Some(format_shell_script_with_rc(
&zsh.shell_path,
&zsh.zshrc_path,
script,
)),
Shell::Bash(bash) => Some(format_shell_script_with_rc(
&bash.shell_path,
&bash.bashrc_path,
script,
)),
Shell::PowerShell(ps) => Some(vec![
ps.exe.clone(),
"-NoProfile".to_string(),
"-Command".to_string(),
script.to_string(),
]),
Shell::Unknown => None,
}
}
pub fn format_default_shell_invocation(&self, command: Vec<String>) -> Option<Vec<String>> {
match self {
Shell::Zsh(zsh) => format_shell_invocation_with_rc(
@@ -113,13 +135,7 @@ fn format_shell_invocation_with_rc(
let joined = strip_bash_lc(command)
.or_else(|| shlex::try_join(command.iter().map(String::as_str)).ok())?;
let rc_command = if std::path::Path::new(rc_path).exists() {
format!("source {rc_path} && ({joined})")
} else {
joined
};
Some(vec![shell_path.to_string(), "-lc".to_string(), rc_command])
Some(format_shell_script_with_rc(shell_path, rc_path, &joined))
}
fn strip_bash_lc(command: &[String]) -> Option<String> {
@@ -135,6 +151,16 @@ fn strip_bash_lc(command: &[String]) -> Option<String> {
}
}
fn format_shell_script_with_rc(shell_path: &str, rc_path: &str, script: &str) -> Vec<String> {
let rc_command = if std::path::Path::new(rc_path).exists() {
format!("source {rc_path} && ({script})")
} else {
script.to_string()
};
vec![shell_path.to_string(), "-lc".to_string(), rc_command]
}
#[cfg(unix)]
fn detect_default_user_shell() -> Shell {
use libc::getpwuid;

View File

@@ -17,3 +17,4 @@ mod seatbelt;
mod stream_error_allows_next_turn;
mod stream_no_completed;
mod user_notification;
mod user_shell_cmd;

View File

@@ -0,0 +1,134 @@
use codex_core::ConversationManager;
use codex_core::NewConversation;
use codex_core::protocol::EventMsg;
use codex_core::protocol::ExecCommandEndEvent;
use codex_core::protocol::Op;
use codex_core::protocol::TurnAbortReason;
use core_test_support::load_default_config_for_test;
use core_test_support::wait_for_event;
use std::path::PathBuf;
use std::process::Command;
use std::process::Stdio;
use tempfile::TempDir;
fn detect_python_executable() -> Option<String> {
let candidates = ["python3", "python"];
candidates.iter().find_map(|candidate| {
Command::new(candidate)
.arg("--version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.ok()
.and_then(|status| status.success().then(|| (*candidate).to_string()))
})
}
#[tokio::test]
async fn user_shell_cmd_ls_and_cat_in_temp_dir() {
let Some(python) = detect_python_executable() else {
eprintln!("skipping test: python3 not found in PATH");
return;
};
// Create a temporary working directory with a known file.
let cwd = TempDir::new().unwrap();
let file_name = "hello.txt";
let file_path: PathBuf = cwd.path().join(file_name);
let contents = "hello from bang test\n";
tokio::fs::write(&file_path, contents)
.await
.expect("write temp file");
// Load config and pin cwd to the temp dir so ls/cat operate there.
let codex_home = TempDir::new().unwrap();
let mut config = load_default_config_for_test(&codex_home);
config.cwd = cwd.path().to_path_buf();
let conversation_manager =
ConversationManager::with_auth(codex_core::CodexAuth::from_api_key("dummy"));
let NewConversation {
conversation: codex,
..
} = conversation_manager
.new_conversation(config)
.await
.expect("create new conversation");
// 1) python should list the file
let list_cmd = format!(
"{python} -c \"import pathlib; print('\\n'.join(sorted(p.name for p in pathlib.Path('.').iterdir())))\""
);
codex
.submit(Op::RunUserShellCommand { command: list_cmd })
.await
.unwrap();
let msg = wait_for_event(&codex, |ev| matches!(ev, EventMsg::ExecCommandEnd(_))).await;
let EventMsg::ExecCommandEnd(ExecCommandEndEvent {
stdout, exit_code, ..
}) = msg
else {
unreachable!()
};
assert_eq!(exit_code, 0);
assert!(
stdout.contains(file_name),
"ls output should include {file_name}, got: {stdout:?}"
);
// 2) python should print the file contents verbatim
let cat_cmd = format!(
"{python} -c \"import pathlib; print(pathlib.Path('{file_name}').read_text(), end='')\""
);
codex
.submit(Op::RunUserShellCommand { command: cat_cmd })
.await
.unwrap();
let msg = wait_for_event(&codex, |ev| matches!(ev, EventMsg::ExecCommandEnd(_))).await;
let EventMsg::ExecCommandEnd(ExecCommandEndEvent {
stdout, exit_code, ..
}) = msg
else {
unreachable!()
};
assert_eq!(exit_code, 0);
assert_eq!(stdout, contents);
}
#[tokio::test]
async fn user_shell_cmd_can_be_interrupted() {
let Some(python) = detect_python_executable() else {
eprintln!("skipping test: python3 not found in PATH");
return;
};
// Set up isolated config and conversation.
let codex_home = TempDir::new().unwrap();
let config = load_default_config_for_test(&codex_home);
let conversation_manager =
ConversationManager::with_auth(codex_core::CodexAuth::from_api_key("dummy"));
let NewConversation {
conversation: codex,
..
} = conversation_manager
.new_conversation(config)
.await
.expect("create new conversation");
// Start a long-running command and then interrupt it.
let sleep_cmd = format!("{python} -c \"import time; time.sleep(5)\"");
codex
.submit(Op::RunUserShellCommand { command: sleep_cmd })
.await
.unwrap();
// Wait until it has started (ExecCommandBegin), then interrupt.
let _ = wait_for_event(&codex, |ev| matches!(ev, EventMsg::ExecCommandBegin(_))).await;
codex.submit(Op::Interrupt).await.unwrap();
// Expect a TurnAborted(Interrupted) notification.
let msg = wait_for_event(&codex, |ev| matches!(ev, EventMsg::TurnAborted(_))).await;
let EventMsg::TurnAborted(ev) = msg else {
unreachable!()
};
assert_eq!(ev.reason, TurnAbortReason::Interrupted);
}

View File

@@ -278,6 +278,7 @@ impl EventProcessor for EventProcessorWithHumanOutput {
command,
cwd,
parsed_cmd: _,
is_user_shell_command: _,
}) => {
self.call_id_to_command.insert(
call_id,

View File

@@ -176,6 +176,16 @@ pub enum Op {
/// Request to shut down codex instance.
Shutdown,
/// Execute a user-initiated one-off shell command (triggered by "!cmd").
///
/// The command string is executed using the user's default shell and may
/// include shell syntax (pipes, redirects, etc.). Output is streamed via
/// `ExecCommand*` events and the UI regains control upon `TaskComplete`.
RunUserShellCommand {
/// The raw command string after '!'
command: String,
},
}
/// Determines the conditions under which the user is consulted to approve
@@ -1052,6 +1062,10 @@ pub struct ExecCommandBeginEvent {
/// The command's working directory if not the default cwd for the agent.
pub cwd: PathBuf,
pub parsed_cmd: Vec<ParsedCommand>,
/// True when this exec was initiated directly by the user (e.g. bang command),
/// not by the agent/model. Defaults to false for backwards compatibility.
#[serde(default)]
pub is_user_shell_command: bool,
}
#[derive(Debug, Clone, Deserialize, Serialize, TS)]

View File

@@ -114,10 +114,14 @@ use codex_git_tooling::restore_ghost_commit;
const MAX_TRACKED_GHOST_COMMITS: usize = 20;
const BANG_COMMAND_HELP_TITLE: &str = "Prefix a command with ! to run it locally";
const BANG_COMMAND_HELP_HINT: &str = "Example: !ls";
// Track information about an in-flight exec command.
struct RunningCommand {
command: Vec<String>,
parsed_cmd: Vec<ParsedCommand>,
is_user_shell_command: bool,
}
const RATE_LIMIT_WARNING_THRESHOLDS: [f64; 3] = [75.0, 90.0, 95.0];
@@ -617,9 +621,9 @@ impl ChatWidget {
pub(crate) fn handle_exec_end_now(&mut self, ev: ExecCommandEndEvent) {
let running = self.running_commands.remove(&ev.call_id);
let (command, parsed) = match running {
Some(rc) => (rc.command, rc.parsed_cmd),
None => (vec![ev.call_id.clone()], Vec::new()),
let (command, parsed, is_user_shell_command) = match running {
Some(rc) => (rc.command, rc.parsed_cmd, rc.is_user_shell_command),
None => (vec![ev.call_id.clone()], Vec::new(), false),
};
let needs_new = self
@@ -633,6 +637,7 @@ impl ChatWidget {
ev.call_id.clone(),
command,
parsed,
is_user_shell_command,
)));
}
@@ -717,6 +722,7 @@ impl ChatWidget {
RunningCommand {
command: ev.command.clone(),
parsed_cmd: ev.parsed_cmd.clone(),
is_user_shell_command: ev.is_user_shell_command,
},
);
if let Some(cell) = self
@@ -727,6 +733,7 @@ impl ChatWidget {
ev.call_id.clone(),
ev.command.clone(),
ev.parsed_cmd.clone(),
ev.is_user_shell_command,
)
{
*cell = new_exec;
@@ -737,6 +744,7 @@ impl ChatWidget {
ev.call_id.clone(),
ev.command.clone(),
ev.parsed_cmd,
ev.is_user_shell_command,
)));
}
@@ -1171,6 +1179,24 @@ impl ChatWidget {
let mut items: Vec<InputItem> = Vec::new();
// Special-case: "!cmd" executes a local shell command instead of sending to the model.
if let Some(stripped) = text.strip_prefix('!') {
let cmd = stripped.trim();
if cmd.is_empty() {
self.app_event_tx.send(AppEvent::InsertHistoryCell(Box::new(
history_cell::new_info_event(
BANG_COMMAND_HELP_TITLE.to_string(),
Some(BANG_COMMAND_HELP_HINT.to_string()),
),
)));
return;
}
self.submit_op(Op::RunUserShellCommand {
command: cmd.to_string(),
});
return;
}
if !text.is_empty() {
items.push(InputItem::Text { text: text.clone() });
}

View File

@@ -525,6 +525,7 @@ fn begin_exec(chat: &mut ChatWidget, call_id: &str, raw_cmd: &str) {
command,
cwd: std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
parsed_cmd,
is_user_shell_command: false,
}),
});
}
@@ -1039,6 +1040,7 @@ async fn binary_size_transcript_snapshot() {
.into_iter()
.map(std::convert::Into::into)
.collect(),
is_user_shell_command: false,
}),
}
}
@@ -2026,6 +2028,7 @@ fn chatwidget_exec_and_status_layout_vt100_snapshot() {
}
.into(),
],
is_user_shell_command: false,
}),
});
chat.handle_codex_event(Event {

View File

@@ -281,6 +281,7 @@ pub(crate) struct ExecCall {
pub(crate) command: Vec<String>,
pub(crate) parsed: Vec<ParsedCommand>,
pub(crate) output: Option<CommandOutput>,
pub(crate) is_user_shell_command: bool,
start_time: Option<Instant>,
duration: Option<Duration>,
}
@@ -493,12 +494,19 @@ impl ExecCell {
body_lines.extend(wrapped_borrowed.iter().map(|l| line_to_static(l)));
}
}
if let Some(output) = call.output.as_ref()
&& output.exit_code != 0
&& (call.is_user_shell_command || output.exit_code != 0)
{
let line_limit = if call.is_user_shell_command {
USER_SHELL_TOOL_CALL_MAX_LINES
} else {
TOOL_CALL_MAX_LINES
};
let out = output_lines(
Some(output),
OutputLinesParams {
line_limit,
only_err: false,
include_angle_pipe: false,
include_prefix: false,
@@ -590,11 +598,13 @@ impl ExecCell {
call_id: String,
command: Vec<String>,
parsed: Vec<ParsedCommand>,
is_user_shell_command: bool,
) -> Option<Self> {
let call = ExecCall {
call_id,
command,
parsed,
is_user_shell_command,
output: None,
start_time: Some(Instant::now()),
duration: None,
@@ -638,6 +648,7 @@ impl HistoryCell for CompletedMcpToolCallWithImageOutput {
const TOOL_CALL_MAX_LINES: usize = 5;
const SESSION_HEADER_MAX_INNER_WIDTH: usize = 56; // Just an eyeballed value
const USER_SHELL_TOOL_CALL_MAX_LINES: usize = 50;
fn card_inner_width(width: u16, max_inner_width: usize) -> Option<usize> {
if width < 4 {
@@ -793,11 +804,13 @@ pub(crate) fn new_active_exec_command(
call_id: String,
command: Vec<String>,
parsed: Vec<ParsedCommand>,
is_user_shell_command: bool,
) -> ExecCell {
ExecCell::new(ExecCall {
call_id,
command,
parsed,
is_user_shell_command,
output: None,
start_time: Some(Instant::now()),
duration: None,
@@ -1590,6 +1603,7 @@ pub(crate) fn new_patch_apply_failure(stderr: String) -> PlainHistoryCell {
formatted_output: String::new(),
}),
OutputLinesParams {
line_limit: TOOL_CALL_MAX_LINES,
only_err: true,
include_angle_pipe: true,
include_prefix: true,
@@ -1667,6 +1681,7 @@ pub(crate) fn new_reasoning_summary_block(
}
struct OutputLinesParams {
line_limit: usize,
only_err: bool,
include_angle_pipe: bool,
include_prefix: bool,
@@ -1674,6 +1689,7 @@ struct OutputLinesParams {
fn output_lines(output: Option<&CommandOutput>, params: OutputLinesParams) -> Vec<Line<'static>> {
let OutputLinesParams {
line_limit,
only_err,
include_angle_pipe,
include_prefix,
@@ -1692,11 +1708,9 @@ fn output_lines(output: Option<&CommandOutput>, params: OutputLinesParams) -> Ve
let src = if *exit_code == 0 { stdout } else { stderr };
let lines: Vec<&str> = src.lines().collect();
let total = lines.len();
let limit = TOOL_CALL_MAX_LINES;
let mut out = Vec::new();
let head_end = total.min(limit);
let head_end = total.min(line_limit);
for (i, raw) in lines[..head_end].iter().enumerate() {
let mut line = ansi_escape_line(raw);
let prefix = if !include_prefix {
@@ -1714,14 +1728,14 @@ fn output_lines(output: Option<&CommandOutput>, params: OutputLinesParams) -> Ve
}
// If we will ellipsize less than the limit, just show it.
let show_ellipsis = total > 2 * limit;
let show_ellipsis = total > 2 * line_limit;
if show_ellipsis {
let omitted = total - 2 * limit;
let omitted = total - 2 * line_limit;
out.push(format!("… +{omitted} lines").into());
}
let tail_start = if show_ellipsis {
total - limit
total - line_limit
} else {
head_end
};
@@ -2127,6 +2141,7 @@ mod tests {
},
],
output: None,
is_user_shell_command: false,
start_time: Some(Instant::now()),
duration: None,
});
@@ -2158,6 +2173,7 @@ mod tests {
cmd: "rg shimmer_spans".into(),
}],
output: None,
is_user_shell_command: false,
start_time: Some(Instant::now()),
duration: None,
});
@@ -2181,6 +2197,7 @@ mod tests {
name: "shimmer.rs".into(),
cmd: "cat shimmer.rs".into(),
}],
false,
)
.unwrap();
cell.complete_call(
@@ -2202,6 +2219,7 @@ mod tests {
name: "status_indicator_widget.rs".into(),
cmd: "cat status_indicator_widget.rs".into(),
}],
false,
)
.unwrap();
cell.complete_call(
@@ -2240,6 +2258,7 @@ mod tests {
},
],
output: None,
is_user_shell_command: false,
start_time: Some(Instant::now()),
duration: None,
});
@@ -2268,6 +2287,7 @@ mod tests {
command: vec!["bash".into(), "-lc".into(), cmd],
parsed: Vec::new(),
output: None,
is_user_shell_command: false,
start_time: Some(Instant::now()),
duration: None,
});
@@ -2298,6 +2318,7 @@ mod tests {
command: vec!["echo".into(), "ok".into()],
parsed: Vec::new(),
output: None,
is_user_shell_command: false,
start_time: Some(Instant::now()),
duration: None,
});
@@ -2326,6 +2347,7 @@ mod tests {
command: vec!["bash".into(), "-lc".into(), long],
parsed: Vec::new(),
output: None,
is_user_shell_command: false,
start_time: Some(Instant::now()),
duration: None,
});
@@ -2353,6 +2375,7 @@ mod tests {
command: vec!["bash".into(), "-lc".into(), cmd],
parsed: Vec::new(),
output: None,
is_user_shell_command: false,
start_time: Some(Instant::now()),
duration: None,
});
@@ -2381,6 +2404,7 @@ mod tests {
command: vec!["bash".into(), "-lc".into(), cmd],
parsed: Vec::new(),
output: None,
is_user_shell_command: false,
start_time: Some(Instant::now()),
duration: None,
});
@@ -2409,6 +2433,7 @@ mod tests {
command: vec!["bash".into(), "-lc".into(), "seq 1 10 1>&2 && false".into()],
parsed: Vec::new(),
output: None,
is_user_shell_command: false,
start_time: Some(Instant::now()),
duration: None,
});
@@ -2455,6 +2480,7 @@ mod tests {
command: vec!["bash".into(), "-lc".into(), long_cmd.to_string()],
parsed: Vec::new(),
output: None,
is_user_shell_command: false,
start_time: Some(Instant::now()),
duration: None,
});

View File

@@ -691,6 +691,7 @@ mod tests {
"exec-1".into(),
vec!["bash".into(), "-lc".into(), "ls".into()],
vec![ParsedCommand::Unknown { cmd: "ls".into() }],
false,
);
exec_cell.complete_call(
"exec-1",