feat: complete Epic 1 production foundation
This commit is contained in:
@@ -7,14 +7,14 @@ use std::sync::{
|
||||
use async_trait::async_trait;
|
||||
use crank_core::{
|
||||
AdapterResponse, CacheScope, CacheStoreError, ConfirmationPolicy, CoordinationStateReservation,
|
||||
CoordinationStateStore, CoordinationStateValue, ExecutionConfig, ExecutionMode, HttpMethod,
|
||||
Operation, OperationId, OperationSafetyClass, OperationSafetyPolicy, OperationSecurityLevel,
|
||||
OperationStatus, Protocol, ProtocolAdapter, ProtocolAdapterError, RestTarget, Target,
|
||||
ToolDescription,
|
||||
CoordinationStateStore, CoordinationStateValue, ExecutionConfig, ExecutionErrorCode,
|
||||
ExecutionMode, HttpMethod, Operation, OperationId, OperationSafetyClass, OperationSafetyPolicy,
|
||||
OperationSecurityLevel, OperationStatus, Protocol, ProtocolAdapter, ProtocolAdapterError,
|
||||
RestTarget, Target, ToolDescription,
|
||||
};
|
||||
use crank_mapping::{MappingRule, MappingSet};
|
||||
use crank_runtime::{
|
||||
InMemoryCoordinationStateStore, RuntimeError, RuntimeExecutorBuilder, RuntimeRequestContext,
|
||||
InMemoryCoordinationStateStore, RuntimeExecutorBuilder, RuntimeRequestContext,
|
||||
};
|
||||
use crank_schema::{Schema, SchemaKind};
|
||||
use futures_util::future::join_all;
|
||||
@@ -42,12 +42,12 @@ async fn destructive_operation_requires_single_use_confirmation() {
|
||||
)
|
||||
.await
|
||||
.expect_err("first destructive call must only stage confirmation");
|
||||
let confirmation_token = match first {
|
||||
RuntimeError::ConfirmationRequired {
|
||||
confirmation_token, ..
|
||||
} => confirmation_token,
|
||||
error => panic!("expected confirmation required, got {error:?}"),
|
||||
};
|
||||
assert_eq!(first.error_code(), ExecutionErrorCode::ConfirmationRequired);
|
||||
let confirmation_token = first
|
||||
.confirmation()
|
||||
.expect("expected confirmation challenge")
|
||||
.token()
|
||||
.to_owned();
|
||||
assert_eq!(call_count.load(Ordering::SeqCst), 0);
|
||||
|
||||
let confirmed_context = context
|
||||
@@ -72,10 +72,7 @@ async fn destructive_operation_requires_single_use_confirmation() {
|
||||
)
|
||||
.await
|
||||
.expect_err("confirmation token must be single-use");
|
||||
assert!(matches!(
|
||||
replay,
|
||||
RuntimeError::InvalidConfirmationToken { .. }
|
||||
));
|
||||
assert_eq!(replay.error_code(), ExecutionErrorCode::ConfirmationInvalid);
|
||||
assert_eq!(call_count.load(Ordering::SeqCst), 1);
|
||||
|
||||
let approved_context = context.with_approval_granted();
|
||||
@@ -111,12 +108,12 @@ async fn confirmation_token_allows_only_one_concurrent_execution() {
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
let RuntimeError::ConfirmationRequired {
|
||||
confirmation_token, ..
|
||||
} = first
|
||||
else {
|
||||
panic!("expected confirmation token")
|
||||
};
|
||||
assert_eq!(first.error_code(), ExecutionErrorCode::ConfirmationRequired);
|
||||
let confirmation_token = first
|
||||
.confirmation()
|
||||
.expect("expected confirmation token")
|
||||
.token()
|
||||
.to_owned();
|
||||
|
||||
let attempts = (0..16).map(|_| {
|
||||
let executor = executor.clone();
|
||||
@@ -143,7 +140,7 @@ async fn confirmation_token_allows_only_one_concurrent_execution() {
|
||||
results
|
||||
.iter()
|
||||
.filter_map(|result| result.as_ref().err())
|
||||
.all(|error| matches!(error, RuntimeError::InvalidConfirmationToken { .. }))
|
||||
.all(|error| error.error_code() == ExecutionErrorCode::ConfirmationInvalid)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -167,11 +164,10 @@ async fn unavailable_confirmation_store_is_preserved_as_a_typed_error() {
|
||||
)
|
||||
.await
|
||||
.expect_err("unavailable store must prevent issuing a token");
|
||||
assert!(matches!(
|
||||
issue_error,
|
||||
RuntimeError::ConfirmationStoreUnavailable { ref operation_id }
|
||||
if operation_id == "op_delete_order"
|
||||
));
|
||||
assert_eq!(
|
||||
issue_error.error_code(),
|
||||
ExecutionErrorCode::SafetyStoreUnavailable
|
||||
);
|
||||
|
||||
let consume_error = executor
|
||||
.execute_with_context(
|
||||
@@ -181,11 +177,10 @@ async fn unavailable_confirmation_store_is_preserved_as_a_typed_error() {
|
||||
)
|
||||
.await
|
||||
.expect_err("unavailable store must not look like an invalid token");
|
||||
assert!(matches!(
|
||||
consume_error,
|
||||
RuntimeError::ConfirmationStoreUnavailable { ref operation_id }
|
||||
if operation_id == "op_delete_order"
|
||||
));
|
||||
assert_eq!(
|
||||
consume_error.error_code(),
|
||||
ExecutionErrorCode::SafetyStoreUnavailable
|
||||
);
|
||||
}
|
||||
|
||||
struct UnavailableCoordinationStore;
|
||||
@@ -396,3 +391,4 @@ fn bool_schema() -> Schema {
|
||||
variants: Vec::new(),
|
||||
}
|
||||
}
|
||||
use crate::support::RuntimeExecutorTestExt;
|
||||
|
||||
@@ -6,15 +6,14 @@ use std::sync::{
|
||||
|
||||
use async_trait::async_trait;
|
||||
use crank_core::{
|
||||
AdapterResponse, ExecutionConfig, ExecutionMode, HttpMethod, IdempotencyMode,
|
||||
IdempotencyPolicy, Operation, OperationId, OperationSecurityLevel, OperationStatus, Protocol,
|
||||
ProtocolAdapter, ProtocolAdapterError, RestTarget, RuntimeRequestContext, Target,
|
||||
ToolDescription,
|
||||
AdapterResponse, ExecutionConfig, ExecutionErrorCode, ExecutionMode, HttpMethod,
|
||||
IdempotencyMode, IdempotencyPolicy, Operation, OperationId, OperationSecurityLevel,
|
||||
OperationStatus, Protocol, ProtocolAdapter, ProtocolAdapterError, RestTarget,
|
||||
RuntimeRequestContext, Target, ToolDescription,
|
||||
};
|
||||
use crank_mapping::{MappingRule, MappingSet};
|
||||
use crank_runtime::{
|
||||
InMemoryCoordinationStateStore, InMemoryResponseCacheStore, RuntimeError,
|
||||
RuntimeExecutorBuilder,
|
||||
InMemoryCoordinationStateStore, InMemoryResponseCacheStore, RuntimeExecutorBuilder,
|
||||
};
|
||||
use crank_schema::{Schema, SchemaKind};
|
||||
use serde_json::json;
|
||||
@@ -71,10 +70,10 @@ async fn required_idempotency_rejects_missing_key_before_adapter_call() {
|
||||
.await
|
||||
.expect_err("missing idempotency key must fail");
|
||||
|
||||
assert!(matches!(
|
||||
error,
|
||||
crank_runtime::RuntimeError::InvalidPreparedRequest { .. }
|
||||
));
|
||||
assert_eq!(
|
||||
error.error_code(),
|
||||
ExecutionErrorCode::PreparedRequestInvalid
|
||||
);
|
||||
assert_eq!(call_count.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
||||
@@ -100,10 +99,10 @@ async fn required_idempotency_fails_closed_without_coordination_store() {
|
||||
.await
|
||||
.expect_err("required idempotency must not execute without an atomic store");
|
||||
|
||||
assert!(matches!(
|
||||
error,
|
||||
RuntimeError::IdempotencyStoreUnavailable { .. }
|
||||
));
|
||||
assert_eq!(
|
||||
error.error_code(),
|
||||
ExecutionErrorCode::SafetyStoreUnavailable
|
||||
);
|
||||
assert_eq!(call_count.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
||||
@@ -186,7 +185,7 @@ async fn same_key_with_different_input_is_rejected() {
|
||||
.await
|
||||
.expect_err("same key must not accept a different request fingerprint");
|
||||
|
||||
assert!(matches!(error, RuntimeError::IdempotencyConflict { .. }));
|
||||
assert_eq!(error.error_code(), ExecutionErrorCode::IdempotencyConflict);
|
||||
assert_eq!(call_count.load(Ordering::SeqCst), 1);
|
||||
}
|
||||
|
||||
@@ -208,16 +207,19 @@ async fn uncertain_adapter_failure_blocks_automatic_retry() {
|
||||
.execute_with_context(&operation, &input, Some(&context))
|
||||
.await
|
||||
.expect_err("adapter failure must be returned");
|
||||
assert!(matches!(first, RuntimeError::ProtocolAdapter(_)));
|
||||
assert_eq!(
|
||||
first.error_code(),
|
||||
ExecutionErrorCode::UpstreamTransportError
|
||||
);
|
||||
|
||||
let retry = executor
|
||||
.execute_with_context(&operation, &input, Some(&context))
|
||||
.await
|
||||
.expect_err("an uncertain external outcome must not be retried automatically");
|
||||
assert!(matches!(
|
||||
retry,
|
||||
RuntimeError::IdempotencyOutcomeUnknown { .. }
|
||||
));
|
||||
assert_eq!(
|
||||
retry.error_code(),
|
||||
ExecutionErrorCode::IdempotencyOutcomeUnknown
|
||||
);
|
||||
assert_eq!(call_count.load(Ordering::SeqCst), 1);
|
||||
}
|
||||
|
||||
@@ -257,9 +259,9 @@ impl ProtocolAdapter for FailingAdapter {
|
||||
_context: &RuntimeRequestContext,
|
||||
) -> Result<AdapterResponse, ProtocolAdapterError> {
|
||||
self.call_count.fetch_add(1, Ordering::SeqCst);
|
||||
Err(ProtocolAdapterError::Message(
|
||||
"upstream outcome is unknown".to_owned(),
|
||||
))
|
||||
Err(ProtocolAdapterError::Transport {
|
||||
dispatch: crank_core::DispatchEvidence::MayHaveDispatched,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,3 +383,4 @@ fn string_schema() -> Schema {
|
||||
variants: Vec::new(),
|
||||
}
|
||||
}
|
||||
use crate::support::RuntimeExecutorTestExt;
|
||||
|
||||
@@ -1,28 +1,60 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use crank_core::{
|
||||
ExecutionConfig, HttpMethod, Operation, OperationId, OperationSecurityLevel, OperationStatus,
|
||||
Protocol, RestTarget, Target, ToolDescription,
|
||||
AdapterResponse, ExecutionConfig, ExecutionMode, HttpMethod, Operation, OperationId,
|
||||
OperationSecurityLevel, OperationStatus, PreparedRequest, Protocol, ProtocolAdapter,
|
||||
ProtocolAdapterError, RestTarget, RuntimeRequestContext as ProtocolRequestContext, Target,
|
||||
ToolDescription,
|
||||
};
|
||||
use crank_mapping::MappingSet;
|
||||
use crank_runtime::RuntimeExecutor;
|
||||
use crank_runtime::RuntimeExecutorBuilder;
|
||||
use crank_schema::{Schema, SchemaKind};
|
||||
use serde_json::json;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
#[test]
|
||||
fn prepares_empty_request_for_no_input_get_with_empty_mapping() {
|
||||
let executor = RuntimeExecutor::new();
|
||||
let operation = no_input_get_operation();
|
||||
use crate::support::RuntimeExecutorTestExt;
|
||||
|
||||
let request = executor
|
||||
.prepare_request(&operation.into(), &json!({}))
|
||||
.unwrap();
|
||||
#[tokio::test]
|
||||
async fn prepares_empty_request_for_no_input_get_with_empty_mapping() {
|
||||
let executor = RuntimeExecutorBuilder::new()
|
||||
.register_adapter(Arc::new(EmptyRequestAdapter))
|
||||
.build();
|
||||
let operation = no_input_get_operation().into();
|
||||
|
||||
assert!(request.path_params.is_empty());
|
||||
assert!(request.query_params.is_empty());
|
||||
assert!(request.headers.is_empty());
|
||||
assert!(request.body.is_none());
|
||||
let response = executor.execute(&operation, &json!({})).await.unwrap();
|
||||
assert_eq!(response, json!({}));
|
||||
}
|
||||
|
||||
struct EmptyRequestAdapter;
|
||||
|
||||
#[async_trait]
|
||||
impl ProtocolAdapter for EmptyRequestAdapter {
|
||||
fn protocol(&self) -> Protocol {
|
||||
Protocol::Rest
|
||||
}
|
||||
|
||||
fn supports_mode(&self, mode: ExecutionMode) -> bool {
|
||||
mode == ExecutionMode::Unary
|
||||
}
|
||||
|
||||
async fn invoke_unary(
|
||||
&self,
|
||||
_target: &Target,
|
||||
request: &PreparedRequest,
|
||||
_context: &ProtocolRequestContext,
|
||||
) -> Result<AdapterResponse, ProtocolAdapterError> {
|
||||
assert!(request.path_params.is_empty());
|
||||
assert!(request.query_params.is_empty());
|
||||
assert!(request.headers.is_empty());
|
||||
assert!(request.body.is_none());
|
||||
Ok(AdapterResponse {
|
||||
status_code: 200,
|
||||
headers: BTreeMap::new(),
|
||||
body: json!({}),
|
||||
data: json!({}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn no_input_get_operation() -> Operation<Schema, MappingSet> {
|
||||
|
||||
Reference in New Issue
Block a user