CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 33m16s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
248 lines
8.2 KiB
Rust
248 lines
8.2 KiB
Rust
//! Initialized HTTP transition against the existing PostgreSQL store, not an SDK session manager.
|
|
use super::*;
|
|
use crate::session::{PostgresTransportSessionStore, TransportSessionStore};
|
|
use crate::session::{SessionState, SessionStoreError};
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use time::OffsetDateTime;
|
|
|
|
struct FaultStore {
|
|
inner: PostgresTransportSessionStore,
|
|
fail_get: AtomicBool,
|
|
fail_mark: AtomicBool,
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
|
impl TransportSessionStore for FaultStore {
|
|
async fn create(
|
|
&self,
|
|
revision: &str,
|
|
workspace: &str,
|
|
agent: &str,
|
|
elicitation: bool,
|
|
now: OffsetDateTime,
|
|
expires: Option<OffsetDateTime>,
|
|
) -> Result<String, SessionStoreError> {
|
|
self.inner
|
|
.create(revision, workspace, agent, elicitation, now, expires)
|
|
.await
|
|
}
|
|
async fn get(&self, id: &str) -> Result<Option<SessionState>, SessionStoreError> {
|
|
if self.fail_get.load(Ordering::SeqCst) {
|
|
return Err(SessionStoreError {
|
|
details: "test read failure".into(),
|
|
});
|
|
}
|
|
self.inner.get(id).await
|
|
}
|
|
async fn mark_initialized(
|
|
&self,
|
|
id: &str,
|
|
now: OffsetDateTime,
|
|
) -> Result<bool, SessionStoreError> {
|
|
if self.fail_mark.load(Ordering::SeqCst) {
|
|
return Err(SessionStoreError {
|
|
details: "test mark failure".into(),
|
|
});
|
|
}
|
|
self.inner.mark_initialized(id, now).await
|
|
}
|
|
async fn delete(&self, id: &str) -> Result<bool, SessionStoreError> {
|
|
self.inner.delete(id).await
|
|
}
|
|
async fn cleanup_expired(&self, now: OffsetDateTime) -> Result<u64, SessionStoreError> {
|
|
self.inner.cleanup_expired(now).await
|
|
}
|
|
async fn active_count(&self, now: OffsetDateTime) -> Result<u64, SessionStoreError> {
|
|
self.inner.active_count(now).await
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn initialized_durable_transition_reconnect_and_store_failures() {
|
|
crank_test_support::postgres_schema_url_for_task("initialized_durable", "t_9c033310").await;
|
|
let registry = test_registry().await;
|
|
let pool = registry.pool().clone();
|
|
let store = Arc::new(FaultStore {
|
|
inner: PostgresTransportSessionStore::from_pool(pool.clone())
|
|
.await
|
|
.unwrap(),
|
|
fail_get: AtomicBool::new(false),
|
|
fail_mark: AtomicBool::new(false),
|
|
});
|
|
let h = Endpoint::started_with_store(Some(store.clone())).await;
|
|
let response = post(&h, None, "application/json", legacy_initialize(80)).await;
|
|
assert_eq!(response.status(), 200);
|
|
let id = response.headers()["MCP-Session-Id"]
|
|
.to_str()
|
|
.unwrap()
|
|
.to_owned();
|
|
let before = store.get(&id).await.unwrap().unwrap();
|
|
assert!(!before.initialized);
|
|
let response = post(
|
|
&h,
|
|
Some(&id),
|
|
"application/json",
|
|
initialized_notification(),
|
|
)
|
|
.await;
|
|
assert_eq!(response.status(), 202);
|
|
assert!(response.bytes().await.unwrap().is_empty());
|
|
assert_eq!(sdk(&h.counters), [1, 1]);
|
|
let restarted = PostgresTransportSessionStore::from_pool(pool.clone())
|
|
.await
|
|
.unwrap();
|
|
let after = restarted.get(&id).await.unwrap().unwrap();
|
|
assert!(after.initialized);
|
|
assert_eq!(after.id, before.id);
|
|
assert_eq!(after.workspace_slug, before.workspace_slug);
|
|
assert_eq!(after.agent_slug, before.agent_slug);
|
|
assert_eq!(after.protocol_version, before.protocol_version);
|
|
assert_eq!(after.created_at, before.created_at);
|
|
assert_eq!(after.expires_at, before.expires_at);
|
|
assert_eq!(after.supports_elicitation, before.supports_elicitation);
|
|
let response = post(
|
|
&h,
|
|
Some(&id),
|
|
"application/json",
|
|
json!({"jsonrpc":"2.0","id":81,"method":"ping"}),
|
|
)
|
|
.await;
|
|
assert_eq!(response.status(), 200);
|
|
assert!(body(response, false).await.get("error").is_none());
|
|
|
|
let pending = store
|
|
.create(
|
|
LEGACY_REVISION,
|
|
WORKSPACE_SLUG,
|
|
AGENT_SLUG,
|
|
false,
|
|
OffsetDateTime::now_utc(),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
store.fail_mark.store(true, Ordering::SeqCst);
|
|
let counts = sdk(&h.counters);
|
|
let response = post(
|
|
&h,
|
|
Some(&pending),
|
|
"application/json",
|
|
initialized_notification(),
|
|
)
|
|
.await;
|
|
assert_eq!(response.status(), 500);
|
|
assert!(response.bytes().await.unwrap().is_empty());
|
|
assert_eq!(sdk(&h.counters), [counts[0] + 1, counts[1] + 1]);
|
|
assert!(!restarted.get(&pending).await.unwrap().unwrap().initialized);
|
|
store.fail_mark.store(false, Ordering::SeqCst);
|
|
|
|
// Fail reads without touching registry/auth tables; admission must stop before the SDK.
|
|
store.fail_get.store(true, Ordering::SeqCst);
|
|
let counts = sdk(&h.counters);
|
|
let response = post(
|
|
&h,
|
|
Some(&pending),
|
|
"application/json",
|
|
initialized_notification(),
|
|
)
|
|
.await;
|
|
assert_eq!(response.status(), 500);
|
|
assert_eq!(sdk(&h.counters), counts);
|
|
store.fail_get.store(false, Ordering::SeqCst);
|
|
assert!(!restarted.get(&pending).await.unwrap().unwrap().initialized);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn initialized_foreign_and_revision_guards_leave_state_untouched() {
|
|
let h = endpoint().await;
|
|
for (revision, workspace, agent) in [
|
|
(LEGACY_REVISION, "foreign-workspace", AGENT_SLUG),
|
|
(LEGACY_REVISION, WORKSPACE_SLUG, "foreign-agent"),
|
|
("2024-11-05", WORKSPACE_SLUG, AGENT_SLUG),
|
|
(MODERN_REVISION, WORKSPACE_SLUG, AGENT_SLUG),
|
|
] {
|
|
let id = h
|
|
.sessions
|
|
.create(
|
|
revision,
|
|
workspace,
|
|
agent,
|
|
false,
|
|
OffsetDateTime::now_utc(),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let state = h.sessions.retained(&id).await.unwrap();
|
|
let ops = h.sessions.snapshot();
|
|
let counts = sdk(&h.counters);
|
|
let response = post(
|
|
&h,
|
|
Some(&id),
|
|
"application/json",
|
|
initialized_notification(),
|
|
)
|
|
.await;
|
|
assert_eq!(
|
|
response.status(),
|
|
if workspace != WORKSPACE_SLUG || agent != AGENT_SLUG {
|
|
404
|
|
} else {
|
|
400
|
|
}
|
|
);
|
|
assert_eq!(h.counters.sdk_initialized_consumed(), counts[1]);
|
|
if workspace == WORKSPACE_SLUG && agent == AGENT_SLUG {
|
|
assert_eq!(h.counters.sdk_initialized_decoded(), counts[0]);
|
|
}
|
|
assert_eq!(h.sessions.snapshot().mark_initialized, ops.mark_initialized);
|
|
assert_eq!(h.sessions.retained(&id).await.unwrap(), state);
|
|
}
|
|
let id = initialize(&h, "application/json").await;
|
|
let counts = sdk(&h.counters);
|
|
let ops = h.sessions.snapshot();
|
|
let response = h
|
|
.request(
|
|
LEGACY_REVISION,
|
|
Some(&id),
|
|
&h.key,
|
|
"application/json",
|
|
initialized_notification(),
|
|
)
|
|
.header("MCP-Session-Id", "conflicting-session")
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status(), 400);
|
|
assert_eq!(sdk(&h.counters), counts);
|
|
assert_eq!(h.sessions.snapshot().mark_initialized, ops.mark_initialized);
|
|
assert!(!h.sessions.retained(&id).await.unwrap().initialized);
|
|
h.registry
|
|
.revoke_platform_api_key_for_agent(
|
|
&workspace_id(),
|
|
&agent_id(),
|
|
&crank_core::PlatformApiKeyId::new("pk_progress_other"),
|
|
&OffsetDateTime::now_utc(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let counts = sdk(&h.counters);
|
|
let ops = h.sessions.snapshot();
|
|
let response = h
|
|
.request(
|
|
LEGACY_REVISION,
|
|
Some(&id),
|
|
&h.other_key,
|
|
"application/json",
|
|
initialized_notification(),
|
|
)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status(), 401);
|
|
assert_eq!(sdk(&h.counters), counts);
|
|
assert_eq!(h.sessions.snapshot().create, ops.create);
|
|
assert_eq!(h.sessions.snapshot().mark_initialized, ops.mark_initialized);
|
|
assert!(!h.sessions.retained(&id).await.unwrap().initialized);
|
|
}
|