mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
core: add wait_for_environment for starting environments (#29745)
## Why With `DeferredExecutor`, a sampling request can begin while an environment is still starting. The model can see that pending state, but needs a way to wait for the environment within the same turn before continuing. Environment startup is owned by Core, so the wait tool should use the same request-frozen `StepContext` that advertised the starting environment. This keeps tool registration and execution tied to the exact startup operation the model saw, even if live thread state later changes. Supersedes #29735. ## What - register `wait_for_environment` when the current `StepContext` contains starting environments - wait on the selected `StartingTurnEnvironment` shared resolution and return a bounded ready or failed result - rebuild the next request normally, removing the wait tool and exposing ready environment tools, or reporting the environment as unavailable after failure ## Testing - `just test -p codex-core deferred_executor_` - verifies the wait tool is replaced by environment-backed tools after startup - verifies startup failure removes both the wait tool and unavailable environment tools while notifying the model
This commit is contained in:
1 parent
1acb722e8a
commit
61ff4d087e
5 files changed
+247
-50
No files matched your search
@@ -35,6 +35,7 @@ pub(crate) mod tool_search_spec;
|
||||
pub(crate) mod unified_exec;
|
||||
mod view_image;
|
||||
pub(crate) mod view_image_spec;
|
||||
mod wait_for_environment;
|
||||
|
||||
use codex_sandboxing::policy_transforms::intersect_permission_profiles;
|
||||
use codex_sandboxing::policy_transforms::merge_permission_profiles;
|
||||
@@ -78,6 +79,7 @@ pub use unified_exec::ExecCommandHandler;
|
||||
pub(crate) use unified_exec::ExecCommandHandlerOptions;
|
||||
pub use unified_exec::WriteStdinHandler;
|
||||
pub use view_image::ViewImageHandler;
|
||||
pub(crate) use wait_for_environment::WaitForEnvironmentHandler;
|
||||
|
||||
pub(crate) fn parse_arguments<T>(arguments: &str) -> Result<T, FunctionCallError>
|
||||
where
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use codex_tools::JsonSchema;
|
||||
use codex_tools::JsonToolOutput;
|
||||
use codex_tools::ResponsesApiTool;
|
||||
use codex_tools::ToolName;
|
||||
use codex_tools::ToolSpec;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::function_tool::FunctionCallError;
|
||||
use crate::tools::context::ToolInvocation;
|
||||
use crate::tools::context::ToolPayload;
|
||||
use crate::tools::context::boxed_tool_output;
|
||||
use crate::tools::handlers::parse_arguments;
|
||||
use crate::tools::registry::CoreToolRuntime;
|
||||
use crate::tools::registry::ToolExecutor;
|
||||
|
||||
const WAIT_FOR_ENVIRONMENT_TOOL_NAME: &str = "wait_for_environment";
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct WaitForEnvironmentArgs {
|
||||
environment_id: String,
|
||||
}
|
||||
|
||||
pub(crate) struct WaitForEnvironmentHandler;
|
||||
|
||||
impl ToolExecutor<ToolInvocation> for WaitForEnvironmentHandler {
|
||||
fn tool_name(&self) -> ToolName {
|
||||
ToolName::plain(WAIT_FOR_ENVIRONMENT_TOOL_NAME)
|
||||
}
|
||||
|
||||
fn spec(&self) -> ToolSpec {
|
||||
ToolSpec::Function(ResponsesApiTool {
|
||||
name: WAIT_FOR_ENVIRONMENT_TOOL_NAME.to_string(),
|
||||
description: "Wait for a starting environment to become available before continuing."
|
||||
.to_string(),
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(
|
||||
BTreeMap::from([(
|
||||
"environment_id".to_string(),
|
||||
JsonSchema::string(Some(
|
||||
"The id of an environment currently marked as starting.".to_string(),
|
||||
)),
|
||||
)]),
|
||||
/*required*/ Some(vec!["environment_id".to_string()]),
|
||||
/*additional_properties*/ Some(false.into()),
|
||||
),
|
||||
output_schema: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn handle(&self, invocation: ToolInvocation) -> codex_tools::ToolExecutorFuture<'_> {
|
||||
Box::pin(async move {
|
||||
let ToolInvocation {
|
||||
payload,
|
||||
step_context,
|
||||
..
|
||||
} = invocation;
|
||||
let arguments = match payload {
|
||||
ToolPayload::Function { arguments } => arguments,
|
||||
_ => {
|
||||
return Err(FunctionCallError::Fatal(format!(
|
||||
"{WAIT_FOR_ENVIRONMENT_TOOL_NAME} handler received unsupported payload"
|
||||
)));
|
||||
}
|
||||
};
|
||||
let args: WaitForEnvironmentArgs = parse_arguments(&arguments)?;
|
||||
let environment_id = args.environment_id;
|
||||
let already_ready = step_context
|
||||
.environments
|
||||
.turn_environments
|
||||
.iter()
|
||||
.any(|environment| environment.environment_id == environment_id);
|
||||
if !already_ready {
|
||||
let Some(environment) = step_context
|
||||
.environments
|
||||
.starting
|
||||
.iter()
|
||||
.find(|environment| environment.selection.environment_id == environment_id)
|
||||
.cloned()
|
||||
else {
|
||||
return Err(FunctionCallError::RespondToModel(format!(
|
||||
"environment `{environment_id}` is neither ready nor starting"
|
||||
)));
|
||||
};
|
||||
|
||||
environment.wait_until_ready().await.map_err(|_| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
"Environment `{environment_id}` failed to start and is unavailable. Continue without it."
|
||||
))
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(boxed_tool_output(JsonToolOutput::new(json!({
|
||||
"environment_id": environment_id,
|
||||
"status": "ready",
|
||||
}))))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl CoreToolRuntime for WaitForEnvironmentHandler {}
|
||||
Reference in new issue
Block a user