mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Preserve one-shot exec when unified exec is disabled (#41393)
## Why Managed configuration can disable resumable unified execution while leaving shell tools enabled. This should not remove command execution entirely or grant access to retained processes. ## What changed - Keep `exec_command` available in completion-only mode when `unified_exec` is disabled. - Remove `tty`, `yield_time_ms`, `session_id`, and `write_stdin` from that mode, and add a `timeout_ms` argument. - Terminate one-shot commands on timeout or turn cancellation, reporting timed-out commands with exit code 124. ## Testing Added integration coverage for tool availability, timeout cleanup, and interruption cleanup under managed configuration. GitOrigin-RevId: a382d34f922fdc4ba52889272c6db985abd42e47
This commit is contained in:
1 parent
4878401e8f
commit
b836aecd4d
10 files changed
+411
-56
No files matched your search
@@ -213,6 +213,7 @@ pub(crate) fn spawn_exit_watcher(
|
||||
.await;
|
||||
} else {
|
||||
let exit_code = process.exit_code().unwrap_or(-1);
|
||||
let timed_out = process.timed_out();
|
||||
finish_and_track_measurements(
|
||||
plugin_metrics_sidecar,
|
||||
exit_code,
|
||||
@@ -233,6 +234,7 @@ pub(crate) fn spawn_exit_watcher(
|
||||
String::new(),
|
||||
exit_code,
|
||||
duration,
|
||||
timed_out,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -344,6 +346,7 @@ pub(crate) async fn emit_exec_end_for_unified_exec(
|
||||
fallback_output: String,
|
||||
exit_code: i32,
|
||||
duration: Duration,
|
||||
timed_out: bool,
|
||||
) {
|
||||
let aggregated_output = resolve_aggregated_output(&transcript, fallback_output).await;
|
||||
let output = ExecToolCallOutput {
|
||||
@@ -352,7 +355,7 @@ pub(crate) async fn emit_exec_end_for_unified_exec(
|
||||
stderr: StreamOutput::new(String::new()),
|
||||
aggregated_output: StreamOutput::new(aggregated_output),
|
||||
duration,
|
||||
timed_out: false,
|
||||
timed_out,
|
||||
};
|
||||
let event_ctx = ToolEventCtx::new(
|
||||
session_ref.as_ref(),
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//!
|
||||
//! Responsibilities
|
||||
//! - Manages interactive processes (create, reuse, buffer output with caps).
|
||||
//! - Supports completion-only calls that terminate on timeout or cancellation.
|
||||
//! - Uses the shared ToolOrchestrator to handle approval, sandbox selection, and
|
||||
//! retry semantics in a single, descriptive flow.
|
||||
//! - Spawns the PTY from a sandbox-transformed `ExecRequest`; on sandbox denial,
|
||||
@@ -49,6 +50,7 @@ use codex_core_plugins::PluginMetricsSidecar;
|
||||
mod async_watcher;
|
||||
mod errors;
|
||||
mod head_tail_buffer;
|
||||
mod oneshot;
|
||||
mod process;
|
||||
mod process_manager;
|
||||
mod process_state;
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
//! One-shot execution over the shared unified-exec lifecycle.
|
||||
//!
|
||||
//! The caller never receives a resumable process. Timeout and cancellation
|
||||
//! terminate the exact process handle published by the normal startup path.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use tokio::time::Duration;
|
||||
use tokio::time::Instant;
|
||||
|
||||
use super::ExecCommandRequest;
|
||||
use super::UnifiedExecContext;
|
||||
use super::UnifiedExecError;
|
||||
use super::UnifiedExecProcess;
|
||||
use super::UnifiedExecProcessManager;
|
||||
use crate::tools::context::ExecCommandToolOutput;
|
||||
|
||||
pub(super) struct Completion<'a> {
|
||||
pub timeout: Duration,
|
||||
pub timed_out: bool,
|
||||
pub process: &'a OnceLock<Arc<UnifiedExecProcess>>,
|
||||
}
|
||||
|
||||
impl UnifiedExecProcessManager {
|
||||
pub(crate) async fn exec_command_to_completion(
|
||||
request: ExecCommandRequest,
|
||||
context: &UnifiedExecContext,
|
||||
timeout: Duration,
|
||||
) -> Result<ExecCommandToolOutput, UnifiedExecError> {
|
||||
let context = UnifiedExecContext::new(
|
||||
Arc::clone(&context.session),
|
||||
Arc::clone(&context.step_context),
|
||||
context.cancellation_token.child_token(),
|
||||
context.call_id.clone(),
|
||||
);
|
||||
let _cancel_on_drop = context.cancellation_token.clone().drop_guard();
|
||||
tokio::spawn(async move {
|
||||
let manager = &context.session.services.unified_exec_manager;
|
||||
let process_id = request.process_id;
|
||||
if Instant::now().checked_add(timeout).is_none() {
|
||||
manager.release_process_id(process_id).await;
|
||||
return Err(UnifiedExecError::process_failed(
|
||||
"timeout_ms is too large".into(),
|
||||
));
|
||||
}
|
||||
|
||||
let process = OnceLock::new();
|
||||
let mut completion = Completion {
|
||||
timeout,
|
||||
timed_out: false,
|
||||
process: &process,
|
||||
};
|
||||
let result = {
|
||||
let mut execution =
|
||||
Box::pin(manager.exec_command_inner(request, &context, Some(&mut completion)));
|
||||
tokio::select! {
|
||||
biased;
|
||||
_ = context.cancellation_token.cancelled() => {
|
||||
if let Some(process) = process.get() {
|
||||
if !process.has_exited()
|
||||
&& let Err(err) = process.terminate_confirmed().await
|
||||
{
|
||||
process.fail_and_terminate(err.to_string());
|
||||
}
|
||||
let _ = execution.await;
|
||||
} else {
|
||||
drop(execution);
|
||||
manager.release_process_id(process_id).await;
|
||||
}
|
||||
Err(UnifiedExecError::process_failed("command cancelled".into()))
|
||||
}
|
||||
result = &mut execution => result,
|
||||
}
|
||||
};
|
||||
|
||||
result.map(|mut output| {
|
||||
if completion.timed_out {
|
||||
output.process_id = None;
|
||||
}
|
||||
output
|
||||
})
|
||||
})
|
||||
.await
|
||||
.map_err(|err| UnifiedExecError::process_failed(err.to_string()))?
|
||||
}
|
||||
}
|
||||
@@ -97,6 +97,7 @@ pub(crate) struct UnifiedExecProcess {
|
||||
state_rx: watch::Receiver<ProcessState>,
|
||||
output_task: Option<JoinHandle<()>>,
|
||||
sandbox_type: SandboxType,
|
||||
timed_out: AtomicBool,
|
||||
_spawn_lifecycle: Option<SpawnLifecycleHandle>,
|
||||
}
|
||||
|
||||
@@ -137,6 +138,7 @@ impl UnifiedExecProcess {
|
||||
state_rx,
|
||||
output_task: None,
|
||||
sandbox_type,
|
||||
timed_out: AtomicBool::new(false),
|
||||
_spawn_lifecycle: spawn_lifecycle,
|
||||
}
|
||||
}
|
||||
@@ -195,6 +197,9 @@ impl UnifiedExecProcess {
|
||||
}
|
||||
|
||||
pub(super) fn exit_code(&self) -> Option<i32> {
|
||||
if self.timed_out() {
|
||||
return Some(124);
|
||||
}
|
||||
let state = self.state_rx.borrow().clone();
|
||||
match &self.process_handle {
|
||||
ProcessHandle::Local(process_handle) => {
|
||||
@@ -204,6 +209,14 @@ impl UnifiedExecProcess {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn mark_timed_out(&self) {
|
||||
self.timed_out.store(true, Ordering::Release);
|
||||
}
|
||||
|
||||
pub(super) fn timed_out(&self) -> bool {
|
||||
self.timed_out.load(Ordering::Acquire)
|
||||
}
|
||||
|
||||
fn finish_termination(&self) {
|
||||
self.output.cancellation_token.cancel();
|
||||
if let Some(output_task) = &self.output_task {
|
||||
|
||||
@@ -12,6 +12,8 @@ use tokio::time::Instant;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::oneshot::Completion;
|
||||
|
||||
use crate::codex_thread::BackgroundTerminalInfo;
|
||||
use crate::exec_env::CODEX_PERMISSION_PROFILE_ENV_VAR;
|
||||
use crate::exec_env::CODEX_THREAD_ID_ENV_VAR;
|
||||
@@ -471,6 +473,16 @@ impl UnifiedExecProcessManager {
|
||||
&self,
|
||||
request: ExecCommandRequest,
|
||||
context: &UnifiedExecContext,
|
||||
) -> Result<ExecCommandToolOutput, UnifiedExecError> {
|
||||
self.exec_command_inner(request, context, /*completion*/ None)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(super) async fn exec_command_inner(
|
||||
&self,
|
||||
request: ExecCommandRequest,
|
||||
context: &UnifiedExecContext,
|
||||
mut completion: Option<&mut Completion<'_>>,
|
||||
) -> Result<ExecCommandToolOutput, UnifiedExecError> {
|
||||
let cwd = request.cwd.clone();
|
||||
let process = self
|
||||
@@ -490,6 +502,9 @@ impl UnifiedExecProcessManager {
|
||||
permissions,
|
||||
} = attempt;
|
||||
let process = Arc::new(process);
|
||||
if let Some(completion) = completion.as_ref() {
|
||||
let _ = completion.process.set(Arc::clone(&process));
|
||||
}
|
||||
let network_denial_monitor = deferred_network_approval.as_ref().map(|deferred| {
|
||||
terminate_process_on_network_denial(
|
||||
Arc::clone(&process),
|
||||
@@ -574,13 +589,28 @@ impl UnifiedExecProcessManager {
|
||||
// For the initial exec_command call, we both stream output to events
|
||||
// (via start_streaming_output above) and collect a snapshot here for
|
||||
// the tool response body.
|
||||
let deadline = start + Duration::from_millis(yield_time_ms);
|
||||
let wait = completion.as_ref().map_or_else(
|
||||
|| Duration::from_millis(yield_time_ms),
|
||||
|completion| completion.timeout,
|
||||
);
|
||||
let deadline = start
|
||||
.checked_add(wait)
|
||||
.ok_or_else(|| UnifiedExecError::process_failed("timeout_ms is too large".into()))?;
|
||||
let collected_output = Self::collect_output_until_deadline(
|
||||
process.output_handles(),
|
||||
Some(context.session.subscribe_elicitation_pause_state()),
|
||||
deadline,
|
||||
)
|
||||
.await;
|
||||
if let Some(completion) = completion.as_mut()
|
||||
&& !process.has_exited()
|
||||
{
|
||||
completion.timed_out = true;
|
||||
process.mark_timed_out();
|
||||
if let Err(err) = process.terminate_confirmed().await {
|
||||
process.fail_and_terminate(err.to_string());
|
||||
}
|
||||
}
|
||||
let wall_time = Instant::now().saturating_duration_since(start);
|
||||
|
||||
let original_token_count = usize::try_from(approx_tokens_from_byte_count(
|
||||
@@ -657,15 +687,20 @@ impl UnifiedExecProcessManager {
|
||||
{
|
||||
return Err(fail_process_with_message(entry.process.as_ref(), message));
|
||||
}
|
||||
process
|
||||
.check_for_sandbox_denial_with_text(&text)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
err.with_output_collection_metadata(
|
||||
original_token_count,
|
||||
output_omitted_bytes,
|
||||
)
|
||||
})?;
|
||||
if !completion
|
||||
.as_ref()
|
||||
.is_some_and(|completion| completion.timed_out)
|
||||
{
|
||||
process
|
||||
.check_for_sandbox_denial_with_text(&text)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
err.with_output_collection_metadata(
|
||||
original_token_count,
|
||||
output_omitted_bytes,
|
||||
)
|
||||
})?;
|
||||
}
|
||||
let metrics_sidecar = entry
|
||||
.plugin_metrics_sidecar
|
||||
.as_ref()
|
||||
@@ -725,6 +760,7 @@ impl UnifiedExecProcessManager {
|
||||
text.clone(),
|
||||
exit,
|
||||
wall_time,
|
||||
process.timed_out(),
|
||||
)
|
||||
.await;
|
||||
|
||||
|
||||
Reference in new issue
Block a user