mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Implemented thread-level atomic elicitation counter for stopwatch pausing
While trying to build out CLI-Tools for the agent to use under skills we
have found that those tools sometimes need to invoke a user elicitation.
These elicitations are handled out of band of the codex app-server but
need to indicate to the exec manager that the command running is
not going to progress on the usual timeout horizon.
Model calls universal exec:
`$ download-credit-card-history --start-date 2026-01-19 --end-date 2026-02-19 > credit_history.jsonl`
download-cred-card-history might hit a hosted/preauthenticated service to fetch data. That service might decide that the request requires an end user approval the access to the personal data. It should be able to signal to the running thread that the command in question is blocked on user elicitation. In that case we want the exec to continue, but the timeout to not expire on the tool call, essentially freezing time until the user approves or rejects the command at which point the tool would signal the app-server to decrement the outstanding elicitation count. Now timeouts would proceed as normal.
- New v2 RPC methods:
- thread/increment_elicitation
- thread/decrement_elicitation
- Protocol updates in:
- codex-rs/app-server-protocol/src/protocol/common.rs
- codex-rs/app-server-protocol/src/protocol/v2.rs
- App-server handlers wired in:
- codex-rs/app-server/src/codex_message_processor.rs
- Counter starts at 0 per thread.
- increment atomically increases the counter.
- decrement atomically decreases the counter; decrement at 0 returns invalid request.
- Transition rules:
- 0 -> 1: broadcast pause state, pausing all active stopwatches immediately.
- >0 -> >0: remain paused.
- 1 -> 0: broadcast unpause state, resuming stopwatches.
- Core thread/session logic:
- codex-rs/core/src/codex_thread.rs
- codex-rs/core/src/codex.rs
- codex-rs/core/src/mcp_connection_manager.rs
- Added centralized stopwatch tracking/controller:
- codex-rs/exec-server/src/posix/stopwatch_controller.rs
- Hooked pause/unpause broadcast handling + stopwatch registration:
- codex-rs/exec-server/src/posix/mcp.rs
- codex-rs/exec-server/src/posix/stopwatch.rs
- codex-rs/exec-server/src/posix.rs
This commit is contained in:
@@ -82,6 +82,7 @@ mod mcp;
|
||||
mod mcp_escalation_policy;
|
||||
mod socket;
|
||||
mod stopwatch;
|
||||
mod stopwatch_controller;
|
||||
|
||||
pub use mcp::ExecResult;
|
||||
|
||||
|
||||
@@ -4,8 +4,11 @@ use std::time::Duration;
|
||||
|
||||
use anyhow::Context as _;
|
||||
use anyhow::Result;
|
||||
use codex_core::MCP_OUT_OF_BAND_ELICITATION_STATE_CAPABILITY;
|
||||
use codex_core::MCP_OUT_OF_BAND_ELICITATION_STATE_METHOD;
|
||||
use codex_core::MCP_SANDBOX_STATE_CAPABILITY;
|
||||
use codex_core::MCP_SANDBOX_STATE_METHOD;
|
||||
use codex_core::OutOfBandElicitationState;
|
||||
use codex_core::SandboxState;
|
||||
use codex_core::protocol::SandboxPolicy;
|
||||
use codex_execpolicy::Policy;
|
||||
@@ -32,11 +35,13 @@ use crate::posix::escalate_server::EscalateServer;
|
||||
use crate::posix::escalate_server::{self};
|
||||
use crate::posix::mcp_escalation_policy::McpEscalationPolicy;
|
||||
use crate::posix::stopwatch::Stopwatch;
|
||||
use crate::posix::stopwatch_controller::StopwatchController;
|
||||
|
||||
/// Path to our patched bash.
|
||||
const CODEX_BASH_PATH_ENV_VAR: &str = "CODEX_BASH_PATH";
|
||||
|
||||
const SANDBOX_STATE_CAPABILITY_VERSION: &str = "1.0.0";
|
||||
const OUT_OF_BAND_ELICITATION_STATE_CAPABILITY_VERSION: &str = "1.0.0";
|
||||
|
||||
pub(crate) fn get_bash_path() -> Result<PathBuf> {
|
||||
std::env::var(CODEX_BASH_PATH_ENV_VAR)
|
||||
@@ -83,6 +88,7 @@ pub struct ExecTool {
|
||||
policy: Arc<RwLock<Policy>>,
|
||||
preserve_program_paths: bool,
|
||||
sandbox_state: Arc<RwLock<Option<SandboxState>>>,
|
||||
stopwatch_controller: StopwatchController,
|
||||
}
|
||||
|
||||
#[tool_router]
|
||||
@@ -100,6 +106,7 @@ impl ExecTool {
|
||||
policy,
|
||||
preserve_program_paths,
|
||||
sandbox_state: Arc::new(RwLock::new(None)),
|
||||
stopwatch_controller: StopwatchController::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +123,7 @@ impl ExecTool {
|
||||
.unwrap_or(codex_core::exec::DEFAULT_EXEC_COMMAND_TIMEOUT_MS),
|
||||
);
|
||||
let stopwatch = Stopwatch::new(effective_timeout);
|
||||
let stopwatch_id = self.stopwatch_controller.register(stopwatch.clone()).await;
|
||||
let cancel_token = stopwatch.cancellation_token();
|
||||
let sandbox_state =
|
||||
self.sandbox_state
|
||||
@@ -141,8 +149,9 @@ impl ExecTool {
|
||||
|
||||
let result = escalate_server
|
||||
.exec(params, cancel_token, &sandbox_state)
|
||||
.await
|
||||
.map_err(|e| McpError::internal_error(e.to_string(), None))?;
|
||||
.await;
|
||||
self.stopwatch_controller.unregister(stopwatch_id).await;
|
||||
let result = result.map_err(|e| McpError::internal_error(e.to_string(), None))?;
|
||||
Ok(CallToolResult::success(vec![Content::json(
|
||||
ExecResult::from(result),
|
||||
)?]))
|
||||
@@ -169,6 +178,15 @@ impl ServerHandler for ExecTool {
|
||||
MCP_SANDBOX_STATE_CAPABILITY.to_string(),
|
||||
sandbox_state_capability,
|
||||
);
|
||||
let mut out_of_band_elicitation_state_capability = JsonObject::new();
|
||||
out_of_band_elicitation_state_capability.insert(
|
||||
"version".to_string(),
|
||||
serde_json::Value::String(OUT_OF_BAND_ELICITATION_STATE_CAPABILITY_VERSION.to_string()),
|
||||
);
|
||||
experimental_capabilities.insert(
|
||||
MCP_OUT_OF_BAND_ELICITATION_STATE_CAPABILITY.to_string(),
|
||||
out_of_band_elicitation_state_capability,
|
||||
);
|
||||
ServerInfo {
|
||||
protocol_version: ProtocolVersion::V_2025_06_18,
|
||||
capabilities: ServerCapabilities::builder()
|
||||
@@ -197,27 +215,47 @@ impl ServerHandler for ExecTool {
|
||||
_context: rmcp::service::RequestContext<rmcp::RoleServer>,
|
||||
) -> Result<CustomResult, McpError> {
|
||||
let CustomRequest { method, params, .. } = request;
|
||||
if method != MCP_SANDBOX_STATE_METHOD {
|
||||
return Err(McpError::method_not_found::<CodexSandboxStateUpdateMethod>());
|
||||
match method.as_ref() {
|
||||
MCP_SANDBOX_STATE_METHOD => {
|
||||
let Some(params) = params else {
|
||||
return Err(McpError::invalid_params(
|
||||
"missing params for sandbox state request".to_string(),
|
||||
None,
|
||||
));
|
||||
};
|
||||
|
||||
let Ok(sandbox_state) = serde_json::from_value::<SandboxState>(params.clone())
|
||||
else {
|
||||
return Err(McpError::invalid_params(
|
||||
"failed to deserialize sandbox state".to_string(),
|
||||
Some(params),
|
||||
));
|
||||
};
|
||||
|
||||
*self.sandbox_state.write().await = Some(sandbox_state);
|
||||
Ok(CustomResult::new(json!({})))
|
||||
}
|
||||
MCP_OUT_OF_BAND_ELICITATION_STATE_METHOD => {
|
||||
let Some(params) = params else {
|
||||
return Err(McpError::invalid_params(
|
||||
"missing params for out-of-band elicitation state request".to_string(),
|
||||
None,
|
||||
));
|
||||
};
|
||||
|
||||
let Ok(state) = serde_json::from_value::<OutOfBandElicitationState>(params.clone())
|
||||
else {
|
||||
return Err(McpError::invalid_params(
|
||||
"failed to deserialize out-of-band elicitation state".to_string(),
|
||||
Some(params),
|
||||
));
|
||||
};
|
||||
|
||||
self.stopwatch_controller.set_paused(state.paused).await;
|
||||
Ok(CustomResult::new(json!({})))
|
||||
}
|
||||
_ => Err(McpError::method_not_found::<CodexSandboxStateUpdateMethod>()),
|
||||
}
|
||||
|
||||
let Some(params) = params else {
|
||||
return Err(McpError::invalid_params(
|
||||
"missing params for sandbox state request".to_string(),
|
||||
None,
|
||||
));
|
||||
};
|
||||
|
||||
let Ok(sandbox_state) = serde_json::from_value::<SandboxState>(params.clone()) else {
|
||||
return Err(McpError::invalid_params(
|
||||
"failed to deserialize sandbox state".to_string(),
|
||||
Some(params),
|
||||
));
|
||||
};
|
||||
|
||||
*self.sandbox_state.write().await = Some(sandbox_state);
|
||||
|
||||
Ok(CustomResult::new(json!({})))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +90,7 @@ impl Stopwatch {
|
||||
result
|
||||
}
|
||||
|
||||
async fn pause(&self) {
|
||||
pub(crate) async fn pause(&self) {
|
||||
let mut guard = self.inner.lock().await;
|
||||
guard.active_pauses += 1;
|
||||
if guard.active_pauses == 1
|
||||
@@ -101,7 +101,7 @@ impl Stopwatch {
|
||||
}
|
||||
}
|
||||
|
||||
async fn resume(&self) {
|
||||
pub(crate) async fn resume(&self) {
|
||||
let mut guard = self.inner.lock().await;
|
||||
if guard.active_pauses == 0 {
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::posix::stopwatch::Stopwatch;
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub(crate) struct StopwatchController {
|
||||
state: Arc<Mutex<StopwatchControllerState>>,
|
||||
operation_lock: Arc<Mutex<()>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct StopwatchControllerState {
|
||||
paused: bool,
|
||||
next_stopwatch_id: u64,
|
||||
stopwatches: HashMap<u64, Stopwatch>,
|
||||
}
|
||||
|
||||
impl StopwatchController {
|
||||
pub(crate) async fn register(&self, stopwatch: Stopwatch) -> u64 {
|
||||
let _operation_guard = self.operation_lock.lock().await;
|
||||
let (stopwatch_id, paused) = {
|
||||
let mut guard = self.state.lock().await;
|
||||
let stopwatch_id = guard.next_stopwatch_id;
|
||||
guard.next_stopwatch_id += 1;
|
||||
guard.stopwatches.insert(stopwatch_id, stopwatch.clone());
|
||||
(stopwatch_id, guard.paused)
|
||||
};
|
||||
|
||||
if paused {
|
||||
stopwatch.pause().await;
|
||||
}
|
||||
|
||||
stopwatch_id
|
||||
}
|
||||
|
||||
pub(crate) async fn unregister(&self, stopwatch_id: u64) {
|
||||
let _operation_guard = self.operation_lock.lock().await;
|
||||
self.state.lock().await.stopwatches.remove(&stopwatch_id);
|
||||
}
|
||||
|
||||
pub(crate) async fn set_paused(&self, paused: bool) {
|
||||
let _operation_guard = self.operation_lock.lock().await;
|
||||
let stopwatches = {
|
||||
let mut guard = self.state.lock().await;
|
||||
if guard.paused == paused {
|
||||
return;
|
||||
}
|
||||
guard.paused = paused;
|
||||
guard.stopwatches.values().cloned().collect::<Vec<_>>()
|
||||
};
|
||||
|
||||
for stopwatch in stopwatches {
|
||||
if paused {
|
||||
stopwatch.pause().await;
|
||||
} else {
|
||||
stopwatch.resume().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::StopwatchController;
|
||||
use crate::posix::stopwatch::Stopwatch;
|
||||
use tokio::time::Duration;
|
||||
use tokio::time::timeout;
|
||||
|
||||
#[tokio::test]
|
||||
async fn pausing_controller_pauses_registered_stopwatch() {
|
||||
let controller = StopwatchController::default();
|
||||
let stopwatch = Stopwatch::new(Duration::from_millis(50));
|
||||
let token = stopwatch.cancellation_token();
|
||||
|
||||
let stopwatch_id = controller.register(stopwatch).await;
|
||||
controller.set_paused(true).await;
|
||||
|
||||
assert!(
|
||||
timeout(Duration::from_millis(30), token.cancelled())
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
|
||||
controller.set_paused(false).await;
|
||||
controller.unregister(stopwatch_id).await;
|
||||
token.cancelled().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn registering_while_paused_starts_paused() {
|
||||
let controller = StopwatchController::default();
|
||||
controller.set_paused(true).await;
|
||||
|
||||
let stopwatch = Stopwatch::new(Duration::from_millis(50));
|
||||
let token = stopwatch.cancellation_token();
|
||||
|
||||
let stopwatch_id = controller.register(stopwatch).await;
|
||||
|
||||
assert!(
|
||||
timeout(Duration::from_millis(30), token.cancelled())
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
|
||||
controller.set_paused(false).await;
|
||||
controller.unregister(stopwatch_id).await;
|
||||
token.cancelled().await;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user