Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(cli): enforce operation wait deadlines
Akshar Patel1f2a9417dba4
diff --git a/apps/cli/src/caps.rs b/apps/cli/src/caps.rsindex a5382c5d9..04b83ca42 100644--- a/apps/cli/src/caps.rs+++ b/apps/cli/src/caps.rs@@ -1861,6 +1861,152 @@ mod tests {);}+ #[tokio::test]+ async fn operation_wait_bounds_slow_responses() {+ for send_headers in [false, true] {+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let address = listener.local_addr().unwrap();+ let server = tokio::spawn(async move {+ let (mut stream, _) = listener.accept().await.unwrap();+ let request = read_request(&mut stream).await;+ assert!(request.starts_with("GET /api/v1/operations/op_test HTTP/1.1\r\n"));+ let body = r#"{"state":"succeeded"}"#;+ if send_headers {+ stream+ .write_all(+ format!(+ "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n",+ body.len()+ )+ .as_bytes(),+ )+ .await+ .unwrap();+ }+ tokio::time::sleep(Duration::from_secs(3)).await;+ if send_headers {+ stream.write_all(body.as_bytes()).await.unwrap();+ } else {+ mock_response(&mut stream, "200 OK", body).await;+ }+ });+ let client =+ AgentClient::new(format!("http://{address}"), "token".to_string()).unwrap();+ let result = tokio::time::timeout(+ Duration::from_millis(1_400),+ crate::jobs::wait_operation(&client, "op_test", 1),+ )+ .await;+ server.abort();+ assert!(server.await.unwrap_err().is_cancelled());+ let error = result+ .expect("operation wait exceeded its deadline")+ .unwrap_err();+ assert_eq!(error.code, "NOT_READY");+ assert_eq!(error.message, "Timed out waiting for the Cap operation");+ assert!(error.retryable);+ assert_eq!(error.retry_after_ms, Some(2_000));+ assert_eq!(error.request_id, None);+ }+ }++ #[tokio::test]+ async fn operation_wait_bounds_poll_sleeps_and_keeps_the_last_request_id() {+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let address = listener.local_addr().unwrap();+ let server = tokio::spawn(async move {+ for request_id in ["request_1", "request_2"] {+ let (mut stream, _) = listener.accept().await.unwrap();+ read_request(&mut stream).await;+ mock_response(+ &mut stream,+ "200 OK",+ &json!({"state":"running", "requestId":request_id}).to_string(),+ )+ .await;+ }+ });+ let client = AgentClient::new(format!("http://{address}"), "token".to_string()).unwrap();+ let result = tokio::time::timeout(+ Duration::from_millis(1_400),+ crate::jobs::wait_operation(&client, "op_test", 1),+ )+ .await;+ server.await.unwrap();+ let error = result+ .expect("operation wait exceeded its deadline")+ .unwrap_err();+ assert_eq!(error.code, "NOT_READY");+ assert_eq!(error.request_id.as_deref(), Some("request_2"));+ }++ #[tokio::test]+ async fn operation_wait_keeps_the_request_id_when_the_next_poll_stalls() {+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let address = listener.local_addr().unwrap();+ let server = tokio::spawn(async move {+ let (mut stream, _) = listener.accept().await.unwrap();+ read_request(&mut stream).await;+ mock_response(+ &mut stream,+ "200 OK",+ r#"{"state":"queued","requestId":"request_1"}"#,+ )+ .await;+ drop(stream);+ let (mut stream, _) = listener.accept().await.unwrap();+ read_request(&mut stream).await;+ tokio::time::sleep(Duration::from_secs(3)).await;+ mock_response(&mut stream, "200 OK", r#"{"state":"succeeded"}"#).await;+ });+ let client = AgentClient::new(format!("http://{address}"), "token".to_string()).unwrap();+ let result = tokio::time::timeout(+ Duration::from_millis(1_400),+ crate::jobs::wait_operation(&client, "op_test", 1),+ )+ .await;+ server.abort();+ assert!(server.await.unwrap_err().is_cancelled());+ let error = result+ .expect("operation wait exceeded its deadline")+ .unwrap_err();+ assert_eq!(error.code, "NOT_READY");+ assert_eq!(error.request_id.as_deref(), Some("request_1"));+ }++ #[tokio::test]+ async fn operation_wait_returns_terminal_results_without_polling() {+ for state in ["succeeded", "failed"] {+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let address = listener.local_addr().unwrap();+ let body = json!({"state":state, "error":{"message":"Operation failed"}});+ let response = body.to_string();+ let server = tokio::spawn(async move {+ let (mut stream, _) = listener.accept().await.unwrap();+ read_request(&mut stream).await;+ mock_response(&mut stream, "200 OK", &response).await;+ });+ let client =+ AgentClient::new(format!("http://{address}"), "token".to_string()).unwrap();+ let result = tokio::time::timeout(+ Duration::from_millis(400),+ crate::jobs::wait_operation(&client, "op_test", 1),+ )+ .await+ .expect("terminal operation should return immediately");+ server.await.unwrap();+ match state {+ "succeeded" => assert_eq!(result.unwrap(), body),+ _ => {+ let error = result.unwrap_err();+ assert_eq!(error.code, "OPERATION_FAILED");+ assert_eq!(error.message, "Operation failed");+ assert!(!error.retryable);+ }+ }+ }+ }+#[tokio::test]async fn mutation_retries_reuse_the_same_idempotency_key() {let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();diff --git a/apps/cli/src/jobs.rs b/apps/cli/src/jobs.rsindex 75373e964..0859c17ed 100644--- a/apps/cli/src/jobs.rs+++ b/apps/cli/src/jobs.rs@@ -60,46 +60,51 @@ pub async fn wait_operation());}let operation_id = opaque_id(operation_id, "Operation ID")?;- let deadline = tokio::time::Instant::now() + Duration::from_secs(timeout);- let mut attempt = 0_u32;- loop {- let operation = client- .get_json(&format!("/operations/{operation_id}"))- .await?;- match operation.get("state").and_then(Value::as_str) {- Some("succeeded") => return Ok(operation),- Some("failed") => {- let message = operation- .get("error")- .and_then(|error| error.get("message"))- .and_then(Value::as_str)- .unwrap_or("Cap operation failed");- return Err(local_error("OPERATION_FAILED", message));- }- Some("queued" | "running") => {}- _ => {- return Err(local_error(- "TEMPORARY_UNAVAILABLE",- "Cap returned an invalid operation state",- ));+ let mut request_id = None;+ let wait = async {+ let mut attempt = 0_u32;+ loop {+ let operation = client+ .get_json(&format!("/operations/{operation_id}"))+ .await?;+ request_id = operation+ .get("requestId")+ .and_then(Value::as_str)+ .map(str::to_string);+ match operation.get("state").and_then(Value::as_str) {+ Some("succeeded") => return Ok(operation),+ Some("failed") => {+ let message = operation+ .get("error")+ .and_then(|error| error.get("message"))+ .and_then(Value::as_str)+ .unwrap_or("Cap operation failed");+ return Err(local_error("OPERATION_FAILED", message));+ }+ Some("queued" | "running") => {}+ _ => {+ return Err(local_error(+ "TEMPORARY_UNAVAILABLE",+ "Cap returned an invalid operation state",+ ));+ }}+ let delay = 500_u64.saturating_mul(1_u64 << attempt.min(4));+ tokio::time::sleep(Duration::from_millis(delay.min(10_000))).await;+ attempt = attempt.saturating_add(1);}- if tokio::time::Instant::now() >= deadline {- return Err(AgentApiError {+ };+ tokio::time::timeout(Duration::from_secs(timeout), wait)+ .await+ .unwrap_or_else(|_| {+ Err(AgentApiError {code: "NOT_READY".to_string(),message: "Timed out waiting for the Cap operation".to_string(),retryable: true,retry_after_ms: Some(2_000),- request_id: operation- .get("requestId")- .and_then(Value::as_str)- .map(str::to_string),- });- }- let delay = 500_u64.saturating_mul(1_u64 << attempt.min(4));- tokio::time::sleep(Duration::from_millis(delay.min(10_000))).await;- attempt = attempt.saturating_add(1);- }+ request_id,+ })+ })}impl JobsArgs {