Skip to content

Commit 3a5ddec

Browse files
committed
refactor: prepare shell-escalation for reusable executors
1 parent 7f75e74 commit 3a5ddec

File tree

7 files changed

+186
-89
lines changed

7 files changed

+186
-89
lines changed

MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codex-rs/shell-escalation/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ libc = { workspace = true }
1414
serde_json = { workspace = true }
1515
path-absolutize = { workspace = true }
1616
serde = { workspace = true, features = ["derive"] }
17-
socket2 = { workspace = true }
17+
socket2 = { workspace = true, features = ["all"] }
1818
tokio = { workspace = true, features = [
1919
"io-std",
20+
"net",
2021
"macros",
2122
"process",
2223
"rt-multi-thread",
2324
"signal",
25+
"time",
2426
] }
2527
tokio-util = { workspace = true }
2628
tracing = { workspace = true }
Lines changed: 110 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,114 @@
11
#[cfg(unix)]
2-
mod unix {
3-
mod escalate_client;
4-
mod escalate_protocol;
5-
mod escalate_server;
6-
mod escalation_policy;
7-
mod socket;
8-
mod stopwatch;
9-
10-
pub use self::escalate_client::run;
11-
pub use self::escalate_protocol::EscalateAction;
12-
pub use self::escalate_server::EscalationPolicyFactory;
13-
pub use self::escalate_server::ExecParams;
14-
pub use self::escalate_server::ExecResult;
15-
pub use self::escalate_server::run_escalate_server;
16-
pub use self::escalation_policy::EscalationPolicy;
17-
pub use self::stopwatch::Stopwatch;
18-
}
2+
pub mod unix;
193

204
#[cfg(unix)]
215
pub use unix::*;
6+
7+
#[cfg(unix)]
8+
pub use unix::escalate_client::run;
9+
#[cfg(unix)]
10+
pub use unix::escalate_protocol::EscalateAction;
11+
#[cfg(unix)]
12+
pub use unix::escalate_server::EscalationPolicyFactory;
13+
#[cfg(unix)]
14+
pub use unix::escalate_server::ExecParams;
15+
#[cfg(unix)]
16+
pub use unix::escalate_server::ExecResult;
17+
#[cfg(unix)]
18+
pub use unix::escalation_policy::EscalationPolicy;
19+
#[cfg(unix)]
20+
pub use unix::stopwatch::Stopwatch;
21+
22+
#[cfg(unix)]
23+
mod legacy_api {
24+
use std::collections::HashMap;
25+
use std::path::Path;
26+
use std::path::PathBuf;
27+
use std::sync::Arc;
28+
use std::time::Duration;
29+
30+
use codex_execpolicy::Policy;
31+
use codex_protocol::config_types::WindowsSandboxLevel;
32+
use codex_protocol::models::SandboxPermissions as ProtocolSandboxPermissions;
33+
use tokio::sync::RwLock;
34+
use tokio_util::sync::CancellationToken;
35+
36+
use crate::unix::escalate_server::EscalationPolicyFactory;
37+
use crate::unix::escalate_server::ExecParams;
38+
use crate::unix::escalate_server::ExecResult;
39+
use crate::unix::escalate_server::SandboxState;
40+
use crate::unix::escalate_server::ShellCommandExecutor;
41+
42+
struct CoreShellCommandExecutor;
43+
44+
#[async_trait::async_trait]
45+
impl ShellCommandExecutor for CoreShellCommandExecutor {
46+
async fn run(
47+
&self,
48+
command: Vec<String>,
49+
cwd: PathBuf,
50+
env: HashMap<String, String>,
51+
cancel_rx: CancellationToken,
52+
sandbox_state: &SandboxState,
53+
) -> anyhow::Result<ExecResult> {
54+
let result = codex_core::exec::process_exec_tool_call(
55+
codex_core::exec::ExecParams {
56+
command,
57+
cwd,
58+
expiration: codex_core::exec::ExecExpiration::Cancellation(cancel_rx),
59+
env,
60+
network: None,
61+
sandbox_permissions: ProtocolSandboxPermissions::UseDefault,
62+
windows_sandbox_level: WindowsSandboxLevel::Disabled,
63+
justification: None,
64+
arg0: None,
65+
},
66+
&sandbox_state.sandbox_policy,
67+
&sandbox_state.sandbox_cwd,
68+
&sandbox_state.codex_linux_sandbox_exe,
69+
sandbox_state.use_linux_sandbox_bwrap,
70+
None,
71+
)
72+
.await?;
73+
74+
Ok(ExecResult {
75+
exit_code: result.exit_code,
76+
output: result.aggregated_output.text,
77+
duration: result.duration,
78+
timed_out: result.timed_out,
79+
})
80+
}
81+
}
82+
83+
#[allow(clippy::too_many_arguments)]
84+
pub async fn run_escalate_server(
85+
exec_params: ExecParams,
86+
sandbox_state: &codex_core::SandboxState,
87+
shell_program: impl AsRef<Path>,
88+
execve_wrapper: impl AsRef<Path>,
89+
policy: Arc<RwLock<Policy>>,
90+
escalation_policy_factory: impl EscalationPolicyFactory,
91+
effective_timeout: Duration,
92+
) -> anyhow::Result<ExecResult> {
93+
let sandbox_state = SandboxState {
94+
sandbox_policy: sandbox_state.sandbox_policy.clone(),
95+
codex_linux_sandbox_exe: sandbox_state.codex_linux_sandbox_exe.clone(),
96+
sandbox_cwd: sandbox_state.sandbox_cwd.clone(),
97+
use_linux_sandbox_bwrap: sandbox_state.use_linux_sandbox_bwrap,
98+
};
99+
crate::unix::escalate_server::run_escalate_server(
100+
exec_params,
101+
&sandbox_state,
102+
shell_program,
103+
execve_wrapper,
104+
policy,
105+
escalation_policy_factory,
106+
effective_timeout,
107+
&CoreShellCommandExecutor,
108+
)
109+
.await
110+
}
111+
}
112+
113+
#[cfg(unix)]
114+
pub use legacy_api::run_escalate_server;

codex-rs/shell-escalation/src/unix/core_shell_escalation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl ShellPolicyFactory {
4040
}
4141
}
4242

43-
struct ShellEscalationPolicy {
43+
pub struct ShellEscalationPolicy {
4444
provider: Arc<dyn ShellActionProvider>,
4545
stopwatch: Stopwatch,
4646
}

0 commit comments

Comments
 (0)