oi_kit_agent/
subprocess.rs1use rmcp::transport::TokioChildProcess;
2use std::io;
3#[cfg(target_os = "linux")]
4use std::sync::{mpsc, OnceLock};
5use tokio::process::ChildStderr;
6use tokio::process::Command;
7
8#[cfg(windows)]
9const CREATE_NO_WINDOW_FLAG: u32 = 0x08000000;
10
11#[cfg(target_os = "linux")]
12fn configure_parent_death_signal(command: &mut Command) {
13 let parent_pid = unsafe { libc::getpid() };
14
15 unsafe {
16 command.pre_exec(move || {
17 if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) != 0 {
18 return Err(std::io::Error::last_os_error());
19 }
20
21 if libc::getppid() != parent_pid {
22 return Err(std::io::Error::from_raw_os_error(libc::ESRCH));
23 }
24
25 Ok(())
26 });
27 }
28}
29
30pub trait SubprocessExt {
31 fn set_no_window(&mut self) -> &mut Self;
32}
33
34pub fn git_command() -> std::process::Command {
37 let mut command = std::process::Command::new("git");
38 command.args([
39 "-c",
40 "safe.bareRepository=explicit",
41 "-c",
42 "core.fsmonitor=false",
43 ]);
44 command
45}
46
47impl SubprocessExt for Command {
48 fn set_no_window(&mut self) -> &mut Self {
49 #[cfg(windows)]
50 {
51 self.creation_flags(CREATE_NO_WINDOW_FLAG);
52 }
53 self
54 }
55}
56
57impl SubprocessExt for std::process::Command {
58 fn set_no_window(&mut self) -> &mut Self {
59 #[cfg(windows)]
60 {
61 use std::os::windows::process::CommandExt;
62 self.creation_flags(CREATE_NO_WINDOW_FLAG);
63 }
64 self
65 }
66}
67
68fn configure_common_subprocess(command: &mut Command) {
69 #[cfg(unix)]
72 command.process_group(0);
73 command.set_no_window();
74}
75
76#[allow(unused_variables)]
77pub fn configure_subprocess(command: &mut Command) {
78 configure_common_subprocess(command);
79 #[cfg(target_os = "linux")]
80 configure_parent_death_signal(command);
81}
82
83#[cfg(target_os = "linux")]
84struct LongLivedSpawnRequest {
85 command: Command,
86 runtime: tokio::runtime::Handle,
87 response: tokio::sync::oneshot::Sender<io::Result<(TokioChildProcess, Option<ChildStderr>)>>,
88}
89
90#[cfg(target_os = "linux")]
91fn long_lived_spawn_sender() -> io::Result<mpsc::Sender<LongLivedSpawnRequest>> {
92 static SENDER: OnceLock<io::Result<mpsc::Sender<LongLivedSpawnRequest>>> = OnceLock::new();
93
94 match SENDER.get_or_init(|| {
95 let (sender, receiver) = mpsc::channel::<LongLivedSpawnRequest>();
96 std::thread::Builder::new()
97 .name("goose-extension-spawner".to_owned())
98 .spawn(move || {
99 while let Ok(mut request) = receiver.recv() {
100 let _runtime_guard = request.runtime.enter();
101 configure_subprocess(&mut request.command);
102 let result = TokioChildProcess::builder(request.command)
103 .stderr(std::process::Stdio::piped())
104 .spawn();
105 let _ = request.response.send(result);
106 }
107 })
108 .map(|_| sender)
109 }) {
110 Ok(sender) => Ok(sender.clone()),
111 Err(error) => Err(io::Error::new(error.kind(), error.to_string())),
112 }
113}
114
115pub async fn spawn_long_lived_mcp_subprocess(
118 command: Command,
119) -> io::Result<(TokioChildProcess, Option<ChildStderr>)> {
120 #[cfg(target_os = "linux")]
121 {
122 let runtime = tokio::runtime::Handle::try_current().map_err(io::Error::other)?;
123 let (response_tx, response_rx) = tokio::sync::oneshot::channel();
124 long_lived_spawn_sender()?
125 .send(LongLivedSpawnRequest {
126 command,
127 runtime,
128 response: response_tx,
129 })
130 .map_err(|_| io::Error::new(io::ErrorKind::BrokenPipe, "extension spawner exited"))?;
131 response_rx
132 .await
133 .map_err(|_| io::Error::new(io::ErrorKind::BrokenPipe, "extension spawner exited"))?
134 }
135
136 #[cfg(not(target_os = "linux"))]
137 {
138 let mut command = command;
139 configure_subprocess(&mut command);
140 TokioChildProcess::builder(command)
141 .stderr(std::process::Stdio::piped())
142 .spawn()
143 }
144}