|
| 1 | +use super::tests::{local_tool_context, remote_tool_context}; |
| 2 | +use super::*; |
| 3 | +use crate::agentic::tools::framework::ToolResult; |
| 4 | +use openbitfun_runtime_ports::*; |
| 5 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 6 | +use tokio_util::sync::CancellationToken; |
| 7 | + |
| 8 | +#[derive(Debug)] |
| 9 | +struct ExecProbe { |
| 10 | + streaming: bool, |
| 11 | + calls: AtomicUsize, |
| 12 | +} |
| 13 | + |
| 14 | +impl RuntimeServicePort for ExecProbe { |
| 15 | + fn capability(&self) -> RuntimeServiceCapability { |
| 16 | + RuntimeServiceCapability::Terminal |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +#[async_trait::async_trait] |
| 21 | +impl TerminalPort for ExecProbe { |
| 22 | + async fn exec_command( |
| 23 | + &self, |
| 24 | + request: TerminalExecCommandRequest, |
| 25 | + ) -> PortResult<TerminalExecCommandResponse> { |
| 26 | + assert_eq!(self.streaming, false); |
| 27 | + assert!(request.tty); |
| 28 | + assert_eq!(request.yield_time_ms, Some(600_000)); |
| 29 | + self.calls.fetch_add(1, Ordering::SeqCst); |
| 30 | + assert!(request.argv.last().unwrap().contains("echo steering-probe")); |
| 31 | + Ok(TerminalExecCommandResponse { |
| 32 | + chunk_id: "chunk".into(), |
| 33 | + wall_time_seconds: 0.01, |
| 34 | + output: "partial".into(), |
| 35 | + session_id: Some(42), |
| 36 | + exit_code: None, |
| 37 | + original_output_chars: 7, |
| 38 | + completion: None, |
| 39 | + wait_interrupted: request |
| 40 | + .wait_interrupt |
| 41 | + .as_ref() |
| 42 | + .is_some_and(CancellationToken::is_cancelled), |
| 43 | + }) |
| 44 | + } |
| 45 | + async fn exec_command_streaming( |
| 46 | + &self, |
| 47 | + request: TerminalExecCommandRequest, |
| 48 | + sink: TerminalExecStreamingOutputSink, |
| 49 | + ) -> PortResult<TerminalExecCommandResponse> { |
| 50 | + assert_eq!(self.streaming, true); |
| 51 | + assert!(request.tty); |
| 52 | + assert_eq!(request.yield_time_ms, Some(600_000)); |
| 53 | + self.calls.fetch_add(1, Ordering::SeqCst); |
| 54 | + assert!(request.argv.last().unwrap().contains("echo steering-probe")); |
| 55 | + sink.send("partial".into()).await.unwrap(); |
| 56 | + Ok(TerminalExecCommandResponse { |
| 57 | + chunk_id: "chunk".into(), |
| 58 | + wall_time_seconds: 0.01, |
| 59 | + output: "partial".into(), |
| 60 | + session_id: Some(42), |
| 61 | + exit_code: None, |
| 62 | + original_output_chars: 7, |
| 63 | + completion: None, |
| 64 | + wait_interrupted: request |
| 65 | + .wait_interrupt |
| 66 | + .as_ref() |
| 67 | + .is_some_and(CancellationToken::is_cancelled), |
| 68 | + }) |
| 69 | + } |
| 70 | + async fn write_stdin( |
| 71 | + &self, |
| 72 | + _: TerminalWriteStdinRequest, |
| 73 | + ) -> PortResult<TerminalExecCommandResponse> { |
| 74 | + panic!("initial wait must not replay input or control the process") |
| 75 | + } |
| 76 | + async fn write_stdin_streaming( |
| 77 | + &self, |
| 78 | + _: TerminalWriteStdinRequest, |
| 79 | + _: TerminalExecStreamingOutputSink, |
| 80 | + ) -> PortResult<TerminalExecCommandResponse> { |
| 81 | + panic!("initial wait must not replay input or control the process") |
| 82 | + } |
| 83 | + async fn send_stdin(&self, _: TerminalSendStdinRequest) -> PortResult<()> { |
| 84 | + panic!("initial wait must not replay input or control the process") |
| 85 | + } |
| 86 | + async fn control_session( |
| 87 | + &self, |
| 88 | + _: TerminalExecControlRequest, |
| 89 | + ) -> PortResult<TerminalExecCommandResponse> { |
| 90 | + panic!("initial wait must not replay input or control the process") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[async_trait::async_trait] |
| 95 | +impl RemoteExecPort for ExecProbe { |
| 96 | + async fn exec_command_once( |
| 97 | + &self, |
| 98 | + _: RemoteExecOneShotCommandRequest, |
| 99 | + ) -> PortResult<RemoteExecOneShotCommandResponse> { |
| 100 | + Ok(RemoteExecOneShotCommandResponse { |
| 101 | + stdout: "/bin/sh\n".into(), |
| 102 | + stderr: String::new(), |
| 103 | + exit_code: 0, |
| 104 | + interrupted: false, |
| 105 | + timed_out: false, |
| 106 | + }) |
| 107 | + } |
| 108 | + async fn exec_command( |
| 109 | + &self, |
| 110 | + request: RemoteExecCommandRequest, |
| 111 | + ) -> PortResult<RemoteExecCommandResponse> { |
| 112 | + if request.max_output_chars.is_some() { |
| 113 | + // Environment capture is a separate best-effort setup command. |
| 114 | + assert!(request.wait_interrupt.is_none()); |
| 115 | + return Err(PortError::new( |
| 116 | + PortErrorKind::NotAvailable, |
| 117 | + "no environment snapshot", |
| 118 | + )); |
| 119 | + } |
| 120 | + assert_eq!(self.streaming, false); |
| 121 | + assert!(request.tty); |
| 122 | + assert_eq!(request.yield_time_ms, Some(600_000)); |
| 123 | + self.calls.fetch_add(1, Ordering::SeqCst); |
| 124 | + assert!(request.command.contains("echo steering-probe")); |
| 125 | + Ok(RemoteExecCommandResponse { |
| 126 | + chunk_id: "chunk".into(), |
| 127 | + wall_time_seconds: 0.01, |
| 128 | + output: "partial".into(), |
| 129 | + session_id: Some(42), |
| 130 | + exit_code: None, |
| 131 | + original_output_chars: 7, |
| 132 | + completion: None, |
| 133 | + wait_interrupted: request |
| 134 | + .wait_interrupt |
| 135 | + .as_ref() |
| 136 | + .is_some_and(CancellationToken::is_cancelled), |
| 137 | + }) |
| 138 | + } |
| 139 | + async fn exec_command_streaming( |
| 140 | + &self, |
| 141 | + request: RemoteExecCommandRequest, |
| 142 | + sink: RemoteExecStreamingOutputSink, |
| 143 | + ) -> PortResult<RemoteExecCommandResponse> { |
| 144 | + assert_eq!(self.streaming, true); |
| 145 | + assert!(request.tty); |
| 146 | + assert_eq!(request.yield_time_ms, Some(600_000)); |
| 147 | + self.calls.fetch_add(1, Ordering::SeqCst); |
| 148 | + assert!(request.command.contains("echo steering-probe")); |
| 149 | + sink.send("partial".into()).await.unwrap(); |
| 150 | + Ok(RemoteExecCommandResponse { |
| 151 | + chunk_id: "chunk".into(), |
| 152 | + wall_time_seconds: 0.01, |
| 153 | + output: "partial".into(), |
| 154 | + session_id: Some(42), |
| 155 | + exit_code: None, |
| 156 | + original_output_chars: 7, |
| 157 | + completion: None, |
| 158 | + wait_interrupted: request |
| 159 | + .wait_interrupt |
| 160 | + .as_ref() |
| 161 | + .is_some_and(CancellationToken::is_cancelled), |
| 162 | + }) |
| 163 | + } |
| 164 | + async fn write_stdin( |
| 165 | + &self, |
| 166 | + _: RemoteWriteStdinRequest, |
| 167 | + ) -> PortResult<RemoteExecCommandResponse> { |
| 168 | + panic!("initial wait must not replay input or control the process") |
| 169 | + } |
| 170 | + async fn write_stdin_streaming( |
| 171 | + &self, |
| 172 | + _: RemoteWriteStdinRequest, |
| 173 | + _: RemoteExecStreamingOutputSink, |
| 174 | + ) -> PortResult<RemoteExecCommandResponse> { |
| 175 | + panic!("initial wait must not replay input or control the process") |
| 176 | + } |
| 177 | + async fn send_stdin(&self, _: RemoteSendStdinRequest) -> PortResult<()> { |
| 178 | + panic!("initial wait must not replay input or control the process") |
| 179 | + } |
| 180 | + async fn control_session( |
| 181 | + &self, |
| 182 | + _: RemoteExecControlRequest, |
| 183 | + ) -> PortResult<RemoteExecCommandResponse> { |
| 184 | + panic!("initial wait must not replay input or control the process") |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +#[test] |
| 189 | +fn exec_command_owns_timeout_and_yields_to_round_injection() { |
| 190 | + let tool = ExecCommandTool::new(); |
| 191 | + assert!(tool.manages_own_execution_timeout()); |
| 192 | + assert!(tool.round_injection_yieldable()); |
| 193 | + assert!(tool.is_concurrency_safe(None)); |
| 194 | + assert!(tool.output_schema()["properties"] |
| 195 | + .get("wait_end_reason") |
| 196 | + .is_some()); |
| 197 | + assert!(!tool.output_schema()["required"] |
| 198 | + .as_array() |
| 199 | + .unwrap() |
| 200 | + .contains(&json!("wait_end_reason"))); |
| 201 | +} |
| 202 | + |
| 203 | +#[tokio::test] |
| 204 | +async fn exec_command_steering_reaches_both_ports_and_preserves_legacy_results() { |
| 205 | + let workspace = tempfile::tempdir().unwrap(); |
| 206 | + for remote in [false, true] { |
| 207 | + for streaming in [false, true] { |
| 208 | + for steered in [false, true] { |
| 209 | + let mut context = if remote { |
| 210 | + remote_tool_context("/home/me/project") |
| 211 | + } else { |
| 212 | + local_tool_context(workspace.path()) |
| 213 | + }; |
| 214 | + context.tool_call_id = |
| 215 | + streaming.then(|| format!("exec-steering-{remote}-{steered}")); |
| 216 | + let probe = Arc::new(ExecProbe { |
| 217 | + streaming, |
| 218 | + calls: AtomicUsize::new(0), |
| 219 | + }); |
| 220 | + let token = CancellationToken::new(); |
| 221 | + token.cancel(); |
| 222 | + context.runtime_handles = context |
| 223 | + .runtime_handles |
| 224 | + .with_round_injection_preemption_token(steered.then_some(token)) |
| 225 | + .with_terminal_port(Some(probe.clone())) |
| 226 | + .with_remote_exec_port(Some(probe.clone())); |
| 227 | + let tool = ExecCommandTool::new(); |
| 228 | + let results = tool |
| 229 | + .call_impl( |
| 230 | + &json!({ |
| 231 | + "cmd":"echo steering-probe", "tty":true, "yield_time_ms":600_000, |
| 232 | + }), |
| 233 | + &context, |
| 234 | + ) |
| 235 | + .await |
| 236 | + .unwrap(); |
| 237 | + assert_eq!(probe.calls.load(Ordering::SeqCst), 1); |
| 238 | + let ToolResult::Result { |
| 239 | + data, |
| 240 | + result_for_assistant, |
| 241 | + .. |
| 242 | + } = &results[0] |
| 243 | + else { |
| 244 | + panic!("steering must return a normal result"); |
| 245 | + }; |
| 246 | + assert_eq!(data["output"], "partial"); |
| 247 | + assert_eq!(data["session_id"], 42); |
| 248 | + assert!(data["exit_code"].is_null()); |
| 249 | + assert!(data["completion"].is_null()); |
| 250 | + let assistant = result_for_assistant.as_deref().unwrap(); |
| 251 | + if steered { |
| 252 | + assert_eq!(data["wait_end_reason"], "steered"); |
| 253 | + assert!(assistant.contains("user steering arrived")); |
| 254 | + assert!(assistant.contains("do not launch the command again")); |
| 255 | + } else { |
| 256 | + assert!(data.get("wait_end_reason").is_none()); |
| 257 | + assert!(!assistant.contains("user steering arrived")); |
| 258 | + } |
| 259 | + let restored: serde_json::Value = |
| 260 | + serde_json::from_str(&serde_json::to_string(data).unwrap()).unwrap(); |
| 261 | + assert_eq!(&restored, data); |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | +} |
0 commit comments