File size: 10,960 Bytes
afa0cbf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | use std::sync::Arc;
use std::sync::PoisonError;
use std::time::Duration;
use anyhow::Context;
use anyhow::Result;
use codex_code_mode::CellId;
use codex_code_mode::CodeModeNestedToolCall;
use codex_code_mode::CodeModeSession;
use codex_code_mode::CodeModeSessionDelegate;
use codex_code_mode::CodeModeSessionProvider;
use codex_code_mode::ExecuteRequest;
use codex_code_mode::FunctionCallOutputContentItem;
use codex_code_mode::GrpcCodeModeSessionProvider;
use codex_code_mode::NotificationFuture;
use codex_code_mode::RuntimeResponse;
use codex_code_mode::ToolInvocationFuture;
use codex_code_mode::WaitOutcome;
use codex_code_mode::WaitRequest;
use pretty_assertions::assert_eq;
use tokio::sync::Semaphore;
use tokio::time::timeout;
use tokio_util::sync::CancellationToken;
#[path = "support/host.rs"]
mod host;
#[path = "support/recording_delegate.rs"]
mod recording_delegate;
use host::HostHarness;
use recording_delegate::RecordingDelegate;
use recording_delegate::cell_id;
const TEST_TIMEOUT: Duration = Duration::from_secs(20);
struct BlockingNotificationDelegate {
started: Semaphore,
release: Semaphore,
delivered: Semaphore,
cancelled: Semaphore,
closed: Semaphore,
}
impl BlockingNotificationDelegate {
fn new() -> Self {
Self {
started: Semaphore::new(/*permits*/ 0),
release: Semaphore::new(/*permits*/ 0),
delivered: Semaphore::new(/*permits*/ 0),
cancelled: Semaphore::new(/*permits*/ 0),
closed: Semaphore::new(/*permits*/ 0),
}
}
}
impl CodeModeSessionDelegate for BlockingNotificationDelegate {
fn invoke_tool<'a>(
&'a self,
_invocation: CodeModeNestedToolCall,
_cancellation: CancellationToken,
) -> ToolInvocationFuture<'a> {
Box::pin(async { Err("unexpected tool invocation".to_string()) })
}
fn notify<'a>(
&'a self,
_call_id: String,
_cell_id: CellId,
_text: String,
cancellation: CancellationToken,
) -> NotificationFuture<'a> {
Box::pin(async move {
self.started.add_permits(/*n*/ 1);
tokio::select! {
_ = cancellation.cancelled() => {
self.cancelled.add_permits(/*n*/ 1);
Err("notification cancelled".to_string())
}
permit = self.release.acquire() => {
permit
.map_err(|_| "notification release closed".to_string())?
.forget();
self.delivered.add_permits(/*n*/ 1);
Ok(())
}
}
})
}
fn cell_closed(&self, _cell_id: &CellId) {
self.closed.add_permits(/*n*/ 1);
}
}
fn request(source: &str) -> ExecuteRequest {
ExecuteRequest {
tool_call_id: "call-1".to_string(),
enabled_tools: Vec::new(),
source: source.to_string(),
yield_time_ms: Some(/*value*/ 5_000),
max_output_tokens: Some(/*value*/ 1_000),
}
}
fn text_response(
cell: &str,
value: &str,
code_mode_host_duration: Option<Duration>,
) -> RuntimeResponse {
RuntimeResponse::Result {
code_mode_host_duration,
cell_id: cell_id(cell),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: value.to_string(),
}],
error_text: None,
}
}
async fn execute(
session: &Arc<dyn CodeModeSession>,
request: ExecuteRequest,
delegate: Arc<dyn CodeModeSessionDelegate>,
) -> Result<RuntimeResponse> {
timeout(TEST_TIMEOUT, async {
session
.execute(request, delegate.clone())
.await
.map_err(anyhow::Error::msg)?
.initial_response()
.await
.map_err(anyhow::Error::msg)
})
.await
.context("timed out executing gRPC code-mode cell")?
}
#[tokio::test]
async fn completed_cells_drain_pending_notifications_before_completion() -> Result<()> {
let host = HostHarness::start("grpc://127.0.0.1:0").await?;
let provider = GrpcCodeModeSessionProvider::new(host.endpoint);
let delegate = Arc::new(BlockingNotificationDelegate::new());
let session = provider
.create_session()
.await
.map_err(anyhow::Error::msg)?;
let executing = Arc::clone(&session);
let execution_delegate = delegate.clone();
let completion = tokio::spawn(async move {
execute(
&executing,
request(r#"notify("notice"); text("done");"#),
execution_delegate,
)
.await
});
timeout(TEST_TIMEOUT, delegate.started.acquire())
.await
.context("notification did not start")??
.forget();
assert!(!completion.is_finished());
delegate.release.add_permits(/*n*/ 1);
let actual = timeout(TEST_TIMEOUT, completion)
.await
.context("completed cell did not finish after notification delivery")???;
assert_eq!(
actual,
text_response("1", "done", actual.code_mode_host_duration())
);
timeout(TEST_TIMEOUT, delegate.delivered.acquire())
.await
.context("completed cell did not deliver its pending notification")??
.forget();
assert!(delegate.cancelled.try_acquire().is_err());
timeout(TEST_TIMEOUT, delegate.closed.acquire())
.await
.context("completed cell was not retired")??
.forget();
let actual = execute(
&session,
request(r#"text("still alive");"#),
delegate.clone(),
)
.await?;
assert_eq!(
actual,
text_response("2", "still alive", actual.code_mode_host_duration())
);
session.shutdown().await.map_err(anyhow::Error::msg)?;
Ok(())
}
#[tokio::test]
async fn completed_waits_drain_pending_notifications_before_returning() -> Result<()> {
let host = HostHarness::start("grpc://127.0.0.1:0").await?;
let provider = GrpcCodeModeSessionProvider::new(host.endpoint);
let delegate = Arc::new(BlockingNotificationDelegate::new());
let session = provider
.create_session()
.await
.map_err(anyhow::Error::msg)?;
let pending = request(r#"yield_control(); notify("notice"); text("done");"#);
let cell = session
.execute(pending, delegate.clone())
.await
.map_err(anyhow::Error::msg)?;
let actual = cell.initial_response().await.map_err(anyhow::Error::msg)?;
assert_eq!(
actual,
RuntimeResponse::Yielded {
code_mode_host_duration: actual.code_mode_host_duration(),
cell_id: cell_id("1"),
content_items: Vec::new(),
}
);
let waiting = Arc::clone(&session);
let completion = tokio::spawn(async move {
waiting
.wait(WaitRequest {
cell_id: cell_id("1"),
yield_time_ms: 5_000,
})
.await
.map_err(anyhow::Error::msg)
});
timeout(TEST_TIMEOUT, delegate.started.acquire())
.await
.context("wait notification did not start")??
.forget();
assert!(!completion.is_finished());
delegate.release.add_permits(/*n*/ 1);
let actual = timeout(TEST_TIMEOUT, completion)
.await
.context("wait did not finish after notification delivery")???;
assert_eq!(
actual,
WaitOutcome::LiveCell(RuntimeResponse::Result {
code_mode_host_duration: actual.code_mode_host_duration(),
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "done".to_string(),
}],
error_text: None,
})
);
timeout(TEST_TIMEOUT, delegate.delivered.acquire())
.await
.context("wait did not deliver its pending notification")??
.forget();
assert!(delegate.cancelled.try_acquire().is_err());
session.shutdown().await.map_err(anyhow::Error::msg)?;
Ok(())
}
#[tokio::test]
async fn termination_cancels_pending_notifications() -> Result<()> {
let host = HostHarness::start("grpc://127.0.0.1:0").await?;
let provider = GrpcCodeModeSessionProvider::new(host.endpoint);
let delegate = Arc::new(BlockingNotificationDelegate::new());
let session = provider
.create_session()
.await
.map_err(anyhow::Error::msg)?;
let mut pending = request(r#"notify("notice"); await new Promise(() => {});"#);
pending.yield_time_ms = Some(/*value*/ 1);
let cell = session
.execute(pending, delegate.clone())
.await
.map_err(anyhow::Error::msg)?;
timeout(TEST_TIMEOUT, delegate.started.acquire())
.await
.context("notification did not start")??
.forget();
let actual = cell.initial_response().await.map_err(anyhow::Error::msg)?;
assert_eq!(
actual,
RuntimeResponse::Yielded {
code_mode_host_duration: actual.code_mode_host_duration(),
cell_id: cell_id("1"),
content_items: Vec::new(),
}
);
let actual = session
.terminate(cell_id("1"))
.await
.map_err(anyhow::Error::msg)?;
assert_eq!(
actual,
WaitOutcome::LiveCell(RuntimeResponse::Terminated {
code_mode_host_duration: actual.code_mode_host_duration(),
cell_id: cell_id("1"),
content_items: Vec::new(),
})
);
timeout(TEST_TIMEOUT, delegate.cancelled.acquire())
.await
.context("termination did not cancel notification delivery")??
.forget();
timeout(TEST_TIMEOUT, delegate.closed.acquire())
.await
.context("terminated cell was not retired")??
.forget();
session.shutdown().await.map_err(anyhow::Error::msg)?;
Ok(())
}
#[tokio::test]
async fn oversized_notification_text_is_delivered_unchanged() -> Result<()> {
let host = HostHarness::start("grpc://127.0.0.1:0").await?;
let provider = GrpcCodeModeSessionProvider::new(host.endpoint);
let delegate = Arc::new(RecordingDelegate::default());
let session = provider
.create_session()
.await
.map_err(anyhow::Error::msg)?;
let actual = execute(
&session,
request(r#"notify("🦀".repeat(512)); text("done");"#),
delegate.clone(),
)
.await?;
assert_eq!(
actual,
text_response("1", "done", actual.code_mode_host_duration())
);
timeout(TEST_TIMEOUT, delegate.notification_delivered.notified())
.await
.context("oversized notification was not delivered")?;
assert_eq!(
*delegate
.notifications
.lock()
.unwrap_or_else(PoisonError::into_inner),
vec![("call-1".to_string(), cell_id("1"), "🦀".repeat(512),)]
);
session.shutdown().await.map_err(anyhow::Error::msg)?;
Ok(())
}
|