use super::*; #[tokio::test] async fn rmcp_http_catalog_follows_new_publication_without_reinitializing() { let h = Harness::new().await; let session = h.initialize().await; let list = json!({"jsonrpc":"2.0","id":60,"method":"tools/list"}); let listed = frames( h.request("2025-11-25", Some(&session), &list) .send() .await .unwrap(), ) .await; assert_eq!( listed.last().unwrap()["result"]["tools"][0]["name"], "create_lead" ); let workspace = crank_core::WorkspaceId::new("ws_default"); let agent = crank_core::AgentId::new("agent_dispatch-observer"); let now = time::OffsetDateTime::now_utc(); let version = crank_core::AgentVersion { agent_id: agent.clone(), version: 2, status: crank_core::AgentStatus::Draft, instructions: json!({}), tool_selection_policy: crank_core::ToolSelectionPolicy::default(), created_at: now, }; let bindings = [crank_core::AgentOperationBinding { operation_id: crank_core::OperationId::new("op_create_lead"), operation_version: 1, tool_name: "new_publication_tool".into(), tool_title: "New Tool".into(), tool_description_override: None, enabled: true, }]; h.state .registry .create_agent_draft_version(crank_registry::CreateAgentDraftVersionRequest { workspace_id: &workspace, agent_id: &agent, version: &version, bindings: &bindings, updated_at: &now, }) .await .unwrap(); h.state .registry .publish_agent(crank_registry::PublishAgentRequest { workspace_id: &workspace, agent_id: &agent, version: 2, published_at: &now, published_by: Some("alice"), expected_state: None, }) .await .unwrap(); let listed = frames( h.request("2025-11-25", Some(&session), &list) .send() .await .unwrap(), ) .await; assert_eq!( listed.last().unwrap()["result"]["tools"][0]["name"], "new_publication_tool" ); let mut message = call(61, "new-publication@example.test", false, false); message["params"]["name"] = json!("new_publication_tool"); let result = frames( h.request("2025-11-25", Some(&session), &message) .send() .await .unwrap(), ) .await; assert_eq!( result.last().unwrap()["result"]["structuredContent"]["id"], "lead_for_new-publication@example.test" ); assert_eq!(h.calls.load(Ordering::SeqCst), 1); } #[tokio::test] async fn rmcp_http_identical_request_ids_in_two_sessions_cancel_independently() { let h = Harness::new().await; let a = h.initialize().await; let b = h.initialize().await; let message = call(62, "slow-same-id@example.test", false, true); let first = h .request("2025-11-25", Some(&a), &message) .send() .await .unwrap(); let second = h .request("2025-11-25", Some(&b), &message) .send() .await .unwrap(); wait_count(&h.calls, 2).await; let cancel = json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":62}}); assert_eq!( h.request("2025-11-25", Some(&a), &cancel) .send() .await .unwrap() .status(), 202 ); wait_count(&h.active, 1).await; assert_eq!( h.request("2025-11-25", Some(&b), &cancel) .send() .await .unwrap() .status(), 202 ); wait_count(&h.active, 0).await; for response in [first, second] { assert_eq!( frames(response).await.last().unwrap()["error"]["message"], "request cancelled" ); } assert_eq!(h.calls.load(Ordering::SeqCst), 2); } // Independent QA: dropped legacy response bodies must not bypass the stream ceiling. #[tokio::test] async fn rmcp_http_qa_legacy_disconnect_does_not_bypass_admission() { let h = Harness::new().await; let session = h.initialize().await; let mut admitted = Vec::new(); for id in 100..117 { let response = h .request( "2025-11-25", Some(&session), &call(id, "slow-qa-limit@example.test", false, true), ) .send() .await .unwrap(); if response.status() == 429 { break; } assert_eq!(response.status(), 200); admitted.push(id); wait_count(&h.calls, admitted.len()).await; drop(response); tokio::time::sleep(std::time::Duration::from_millis(20)).await; } let active_before_cleanup = h.active.load(Ordering::SeqCst); let free_slots = h.state.session_slots.available_permits(); let calls_before_cleanup = h.calls.load(Ordering::SeqCst); for id in &admitted { let cancel = json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":id}}); assert_eq!( h.request("2025-11-25", Some(&session), &cancel) .send() .await .unwrap() .status(), 202 ); } wait_count(&h.active, 0).await; assert!( admitted.len() <= 16 && calls_before_cleanup <= 16 && active_before_cleanup <= 16, "legacy disconnect bypassed admission: admitted={}, calls={calls_before_cleanup}, active={active_before_cleanup}, free_slots={free_slots}", admitted.len() ); tokio::time::timeout(std::time::Duration::from_secs(2), async { while h.state.session_slots.available_permits() != 16 { tokio::time::sleep(std::time::Duration::from_millis(10)).await; } }) .await .expect("owned cancellation cleanup must release all admission permits"); assert_eq!(h.state.session_slots.available_permits(), 16); // After cleanup, normal admission and REST execution must work again. let calls_before = h.calls.load(Ordering::SeqCst); let message = call(199, "recovery-after-cleanup@example.test", false, false); let response = h .request("2025-11-25", Some(&session), &message) .send() .await .unwrap(); assert_eq!(response.status(), 200); let result = frames(response).await; assert_eq!( result.last().unwrap()["result"]["structuredContent"]["id"], "lead_for_recovery-after-cleanup@example.test" ); assert_eq!(h.calls.load(Ordering::SeqCst), calls_before + 1); } #[tokio::test] async fn rmcp_http_correlation_headers_preserve_each_concurrent_request() { let h = Harness::new().await; let first = call(50, "correlation-first@example.test", true, false); let second = call(51, "correlation-second@example.test", true, false); let (a, b) = tokio::join!( h.request("2026-07-28", None, &first) .header("x-request-id", "rmcp-oracle-first") .send(), h.request("2026-07-28", None, &second) .header("x-request-id", "rmcp-oracle-second") .send() ); let a = a.unwrap(); let b = b.unwrap(); assert_eq!(a.headers()["x-request-id"], "rmcp-oracle-first"); assert_eq!(b.headers()["x-request-id"], "rmcp-oracle-second"); assert_eq!( frames(a).await[0]["result"]["structuredContent"]["id"], "lead_for_correlation-first@example.test" ); assert_eq!( frames(b).await[0]["result"]["structuredContent"]["id"], "lead_for_correlation-second@example.test" ); } #[tokio::test] async fn rmcp_http_stream_limit_rejects_without_dispatch_then_recovers() { let h = Harness::new().await; let permits = h .state .session_slots .clone() .acquire_many_owned(16) .await .unwrap(); let message = call(40, "limit@example.test", true, false); assert_eq!( h.request("2026-07-28", None, &message) .send() .await .unwrap() .status(), 429 ); assert_eq!(h.calls.load(Ordering::SeqCst), 0); drop(permits); let response = h .request("2026-07-28", None, &message) .send() .await .unwrap(); assert_eq!( frames(response).await[0]["result"]["structuredContent"]["id"], "lead_for_limit@example.test" ); assert_eq!(h.calls.load(Ordering::SeqCst), 1); } #[tokio::test] async fn rmcp_http_rate_limit_is_existing_admission_not_sdk_default() { let h = Harness::with_rate(1).await; let list = json!({"jsonrpc":"2.0","id":41,"method":"tools/list","params":{"_meta":meta()}}); assert_eq!( h.request("2026-07-28", None, &list) .send() .await .unwrap() .status(), 200 ); assert_eq!( h.request( "2026-07-28", None, &call(42, "rate@example.test", true, false) ) .send() .await .unwrap() .status(), 429 ); assert_eq!(h.calls.load(Ordering::SeqCst), 0); } #[tokio::test] async fn rmcp_http_current_durable_store_survives_recreation_but_is_not_sdk_store() { use crate::session::{PostgresTransportSessionStore, TransportSessionStore}; let h = Harness::new().await; let store = PostgresTransportSessionStore::from_pool(h.state.registry.pool().clone()) .await .unwrap(); let now = time::OffsetDateTime::now_utc(); let id = store .create( "2025-11-25", "default", "dispatch-observer", false, now, Some(now + time::Duration::hours(24)), ) .await .unwrap(); assert!(store.mark_initialized(&id, now).await.unwrap()); drop(store); let restarted = PostgresTransportSessionStore::from_pool(h.state.registry.pool().clone()) .await .unwrap(); let persisted = restarted.get(&id).await.unwrap().unwrap(); assert!(persisted.initialized); assert_eq!(persisted.workspace_slug, "default"); assert_eq!(persisted.protocol_version, "2025-11-25"); assert_eq!( h.request( "2025-11-25", Some(&id), &call(43, "durable@example.test", false, false) ) .send() .await .unwrap() .status(), 404 ); assert!( restarted.get(&id).await.unwrap().is_some(), "SDK experiment never mutates existing storage" ); assert_eq!(h.calls.load(Ordering::SeqCst), 0); } #[tokio::test] async fn rmcp_http_json_modern_and_legacy_sse_only_gap_are_observed() { let h = Harness::new().await; let session = h.initialize().await; let response = h .request( "2026-07-28", None, &call(44, "json@example.test", true, false), ) .send() .await .unwrap(); assert!( response.headers()["content-type"] .to_str() .unwrap() .starts_with("application/json") ); assert_eq!( frames(response).await[0]["result"]["structuredContent"]["id"], "lead_for_json@example.test" ); // Build a JSON-only request (not two contradictory Accept carriers). let response = reqwest::Client::new() .post(format!("{}/v1/default/dispatch-observer", h.server.1)) .bearer_auth(&h.key) .header("Accept", "application/json") .header("MCP-Protocol-Version", "2025-11-25") .header("MCP-Session-Id", &session) .json(&call(45, "legacy-json@example.test", false, false)) .send() .await .unwrap(); assert_eq!( response.status(), 406, "bundled SDK legacy mode requires SSE Accept; not transport parity" ); assert_eq!(h.calls.load(Ordering::SeqCst), 1); } #[tokio::test] async fn rmcp_http_concurrent_keys_keep_outputs_and_cancel_ownership_separate() { let h = Harness::new().await; let session = h.initialize().await; sqlx::query("insert into platform_api_keys (id, workspace_id, agent_id, key_kind, name, prefix, secret_hash, scopes_json, status, created_at) select 'pk_second', workspace_id, agent_id, key_kind, 'second', prefix, $1, scopes_json, status, created_at from platform_api_keys where id = 'pk_dispatch_observer'") .bind(crate::access::hash_access_secret("test-second-valid-key")).execute(h.state.registry.pool()).await.unwrap(); let first = call(46, "first@example.test", false, false); let second = call(47, "second@example.test", false, false); let (a, b) = tokio::join!( h.request("2025-11-25", Some(&session), &first).send(), h.request_as( "2025-11-25", Some(&session), &second, "test-second-valid-key" ) .send() ); assert_eq!( frames(a.unwrap()).await.last().unwrap()["result"]["structuredContent"]["id"], "lead_for_first@example.test" ); assert_eq!( frames(b.unwrap()).await.last().unwrap()["result"]["structuredContent"]["id"], "lead_for_second@example.test" ); assert_eq!(h.calls.load(Ordering::SeqCst), 2); }