исправить: закрыть ревью сквозной корреляции
CI / Rust Checks (pull_request) Successful in 8m33s
CI / UI Checks (pull_request) Successful in 5s
CI / Frontend E2E (pull_request) Successful in 6m51s
CI / Community Image Smoke (pull_request) Failing after 9m10s
CI / Deploy (pull_request) Has been skipped

This commit is contained in:
2026-07-31 02:37:45 +03:00
parent 0e8f1ca03a
commit 9a7d60593a
28 changed files with 667 additions and 98 deletions
@@ -1,426 +0,0 @@
use std::{
collections::BTreeMap,
sync::{Arc, Mutex},
};
use async_trait::async_trait;
use crank_core::{
AdapterResponse, ConfirmationPolicy, ExecutionConfig, ExecutionMode, HttpMethod,
IdempotencyMode, IdempotencyPolicy, Operation, OperationId, OperationSafetyClass,
OperationSafetyPolicy, OperationSecurityLevel, OperationStatus, Protocol, ProtocolAdapter,
ProtocolAdapterError, RestTarget, Target, ToolDescription,
};
use crank_mapping::{MappingRule, MappingSet};
use crank_runtime::{
InMemoryCoordinationStateStore, RuntimeError, RuntimeExecutorBuilder, RuntimeRequestContext,
};
use crank_schema::{Schema, SchemaKind};
use crank_trace::{Stage, StageOutcome};
use serde_json::json;
use time::OffsetDateTime;
use tracing::{Id, Instrument, Subscriber, field::Visit, instrument::WithSubscriber};
use tracing_subscriber::{Layer, layer::SubscriberExt, registry::LookupSpan};
#[tokio::test]
async fn successful_execution_has_real_stages_and_omits_inapplicable_ones() {
let capture = TraceCapture::default();
let subscriber = tracing_subscriber::registry().with(capture.clone());
let executor = RuntimeExecutorBuilder::new()
.register_adapter(Arc::new(SuccessAdapter))
.build();
let operation = operation().into();
let context = RuntimeRequestContext::from_request_id("req_stage_test");
let result = async {
let root = tracing::info_span!(target: "crank::trace", "mcp.request");
executor
.execute_with_context(
&operation,
&json!({"name": "canary-secret"}),
Some(&context),
)
.instrument(root)
.await
}
.with_subscriber(subscriber)
.await;
assert_eq!(result.unwrap(), json!({"accepted": true}));
let spans = capture.snapshot();
assert_stage(&spans, "runtime.execute", "success");
assert_stage(&spans, "runtime.arguments.map", "success");
assert_stage(&spans, "upstream.http", "success");
assert_stage(&spans, "runtime.response.transform", "success");
assert!(!spans.iter().any(|span| span.name == "approval.check"));
assert!(!spans.iter().any(|span| span.name == "runtime.idempotency"));
assert!(
spans
.iter()
.flat_map(|span| span.fields.values())
.all(|value| !value.contains("canary-secret"))
);
let runtime = spans
.iter()
.find(|span| span.name == "runtime.execute")
.expect("runtime span");
assert_eq!(runtime.parent_name, Some("mcp.request"));
for child in [
"runtime.arguments.map",
"upstream.http",
"runtime.response.transform",
] {
assert_eq!(
spans
.iter()
.find(|span| span.name == child)
.and_then(|span| span.parent_name),
Some("runtime.execute"),
"{child} must be a runtime child"
);
}
}
#[tokio::test]
async fn failed_mapping_records_closed_category_and_stops_later_stages() {
let capture = TraceCapture::default();
let subscriber = tracing_subscriber::registry().with(capture.clone());
let executor = RuntimeExecutorBuilder::new()
.register_adapter(Arc::new(SuccessAdapter))
.build();
let operation = operation().into();
let result = async { executor.execute(&operation, &json!({})).await }
.with_subscriber(subscriber)
.await;
assert!(result.is_err());
let spans = capture.snapshot();
let runtime = spans
.iter()
.find(|span| span.name == "runtime.execute")
.expect("runtime span");
assert_eq!(runtime.fields["outcome"], "error");
assert_eq!(runtime.fields["error.category"], "schema");
assert_stage(&spans, "runtime.arguments.map", "error");
assert!(!spans.iter().any(|span| span.name == "upstream.http"));
assert!(
!spans
.iter()
.any(|span| span.name == "runtime.response.transform")
);
}
#[tokio::test]
async fn approval_stage_is_present_only_when_confirmation_is_required() {
let capture = TraceCapture::default();
let subscriber = tracing_subscriber::registry().with(capture.clone());
let executor = RuntimeExecutorBuilder::new()
.register_adapter(Arc::new(SuccessAdapter))
.with_coordination_store(Arc::new(InMemoryCoordinationStateStore::default()))
.build();
let mut source = operation();
source.execution_config.safety = Some(OperationSafetyPolicy {
class: OperationSafetyClass::Destructive,
confirmation: Some(ConfirmationPolicy { ttl_ms: 60_000 }),
});
let operation = source.into();
let context = RuntimeRequestContext::from_request_id("req_approval_stage")
.with_response_cache_scope("workspace", "agent");
let result = async {
executor
.execute_with_context(
&operation,
&json!({"name": "requires-confirmation"}),
Some(&context),
)
.await
}
.with_subscriber(subscriber)
.await;
assert!(matches!(
result,
Err(RuntimeError::ConfirmationRequired { .. })
));
let spans = capture.snapshot();
assert_stage(&spans, "approval.check", "required");
assert!(!spans.iter().any(|span| span.name == "upstream.http"));
assert!(!spans.iter().any(|span| span.name == "runtime.idempotency"));
}
#[tokio::test]
async fn idempotency_stage_distinguishes_execution_from_replay() {
let capture = TraceCapture::default();
let subscriber = tracing_subscriber::registry().with(capture.clone());
let executor = RuntimeExecutorBuilder::new()
.register_adapter(Arc::new(SuccessAdapter))
.with_coordination_store(Arc::new(InMemoryCoordinationStateStore::default()))
.build();
let mut source = operation();
source.execution_config.idempotency = Some(IdempotencyPolicy {
mode: IdempotencyMode::Required,
ttl_ms: 60_000,
input_field: Some("name".to_owned()),
header_name: Some("Idempotency-Key".to_owned()),
});
let operation = source.into();
let context = RuntimeRequestContext::from_request_id("req_idempotency_stage")
.with_response_cache_scope("workspace", "agent");
let (first, replay) = async {
let first = executor
.execute_with_context(&operation, &json!({"name": "stable-key"}), Some(&context))
.await;
let replay = executor
.execute_with_context(&operation, &json!({"name": "stable-key"}), Some(&context))
.await;
(first, replay)
}
.with_subscriber(subscriber)
.await;
assert!(first.is_ok());
assert!(replay.is_ok());
let spans = capture.snapshot();
let idempotency_outcomes = spans
.iter()
.filter(|span| span.name == "runtime.idempotency")
.map(|span| span.fields["outcome"].as_str())
.collect::<Vec<_>>();
assert!(idempotency_outcomes.contains(&"execute"));
assert!(idempotency_outcomes.contains(&"replay"));
assert_eq!(
spans
.iter()
.filter(|span| span.name == "upstream.http")
.count(),
1,
"replay must not pretend to call upstream"
);
}
fn assert_stage(spans: &[CapturedSpan], name: &str, outcome: &str) {
let span = spans
.iter()
.find(|span| span.name == name)
.unwrap_or_else(|| panic!("missing stage {name}"));
assert_eq!(span.fields["outcome"], outcome);
}
struct SuccessAdapter;
#[async_trait]
impl ProtocolAdapter for SuccessAdapter {
fn protocol(&self) -> Protocol {
Protocol::Rest
}
fn supports_mode(&self, mode: ExecutionMode) -> bool {
mode == ExecutionMode::Unary
}
async fn invoke_unary(
&self,
_target: &Target,
_prepared: &crank_core::PreparedRequest,
_context: &crank_core::RuntimeRequestContext,
) -> Result<AdapterResponse, ProtocolAdapterError> {
let span = Stage::UpstreamHttp.span();
let response = Ok(AdapterResponse {
status_code: 200,
headers: BTreeMap::new(),
body: json!({"accepted": true}),
data: json!({"accepted": true}),
});
StageOutcome::Success.record(&span);
response
}
}
fn operation() -> Operation<Schema, MappingSet> {
Operation {
id: OperationId::new("op_stage_test"),
name: "stage_test".to_owned(),
display_name: "Stage test".to_owned(),
category: "test".to_owned(),
protocol: Protocol::Rest,
security_level: OperationSecurityLevel::Standard,
status: OperationStatus::Published,
version: 1,
target: Target::Rest(RestTarget {
base_url: "https://example.invalid".to_owned(),
method: HttpMethod::Post,
path_template: "/test".to_owned(),
static_headers: BTreeMap::new(),
}),
input_schema: object_schema(BTreeMap::from([("name".to_owned(), string_schema())])),
output_schema: object_schema(BTreeMap::from([("accepted".to_owned(), bool_schema())])),
input_mapping: MappingSet {
rules: vec![MappingRule {
source: "$.mcp.name".to_owned(),
target: "$.request.body.name".to_owned(),
required: true,
default_value: None,
transform: None,
condition: None,
notes: None,
}],
},
output_mapping: MappingSet {
rules: vec![MappingRule {
source: "$.response.body.accepted".to_owned(),
target: "$.output.accepted".to_owned(),
required: true,
default_value: None,
transform: None,
condition: None,
notes: None,
}],
},
execution_config: ExecutionConfig {
timeout_ms: 1_000,
retry_policy: None,
response_cache: None,
idempotency: None,
safety: None,
approval_policy: None,
auth_profile_ref: None,
headers: BTreeMap::new(),
},
tool_description: ToolDescription {
title: "Stage test".to_owned(),
description: "Tests trace stages.".to_owned(),
tags: Vec::new(),
examples: Vec::new(),
},
samples: None,
generated_draft: None,
config_export: None,
wizard_state: None,
created_at: OffsetDateTime::UNIX_EPOCH,
updated_at: OffsetDateTime::UNIX_EPOCH,
published_at: None,
}
}
fn object_schema(fields: BTreeMap<String, Schema>) -> Schema {
Schema {
kind: SchemaKind::Object,
description: None,
required: true,
nullable: false,
default_value: None,
fields,
items: None,
enum_values: Vec::new(),
variants: Vec::new(),
}
}
fn string_schema() -> Schema {
Schema {
kind: SchemaKind::String,
description: None,
required: true,
nullable: false,
default_value: None,
fields: BTreeMap::new(),
items: None,
enum_values: Vec::new(),
variants: Vec::new(),
}
}
fn bool_schema() -> Schema {
Schema {
kind: SchemaKind::Boolean,
..string_schema()
}
}
#[derive(Clone, Default)]
struct TraceCapture {
spans: Arc<Mutex<Vec<CapturedSpan>>>,
}
impl TraceCapture {
fn snapshot(&self) -> Vec<CapturedSpan> {
self.spans.lock().expect("span lock").clone()
}
}
#[derive(Clone, Debug)]
struct CapturedSpan {
name: &'static str,
parent_name: Option<&'static str>,
fields: BTreeMap<String, String>,
}
impl<S> Layer<S> for TraceCapture
where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
{
fn on_new_span(
&self,
attributes: &tracing::span::Attributes<'_>,
id: &Id,
context: tracing_subscriber::layer::Context<'_, S>,
) {
let parent = attributes
.parent()
.and_then(|parent| context.span(parent))
.or_else(|| {
attributes
.is_contextual()
.then(|| context.lookup_current())
.flatten()
});
let mut visitor = FieldVisitor::default();
attributes.record(&mut visitor);
let mut spans = self.spans.lock().expect("span lock");
let index = spans.len();
spans.push(CapturedSpan {
name: attributes.metadata().name(),
parent_name: parent.map(|span| span.metadata().name()),
fields: visitor.fields,
});
context
.span(id)
.expect("span exists")
.extensions_mut()
.insert(index);
}
fn on_record(
&self,
id: &Id,
values: &tracing::span::Record<'_>,
context: tracing_subscriber::layer::Context<'_, S>,
) {
let mut visitor = FieldVisitor::default();
values.record(&mut visitor);
let span = context.span(id).expect("span exists");
let index = *span.extensions().get::<usize>().expect("capture index");
self.spans.lock().expect("span lock")[index]
.fields
.extend(visitor.fields);
}
}
#[derive(Default)]
struct FieldVisitor {
fields: BTreeMap<String, String>,
}
impl Visit for FieldVisitor {
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
self.fields
.insert(field.name().to_owned(), value.to_owned());
}
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
self.fields
.insert(field.name().to_owned(), format!("{value:?}"));
}
}
@@ -25,8 +25,12 @@ async fn valkey_coordination_and_rate_limit_operations_are_atomic() {
.get_host_port_ipv4(6379.tcp())
.await
.expect("Valkey port must be mapped");
let host = container
.get_host()
.await
.expect("Docker host must be resolved");
let store = Arc::new(
RedisCacheStore::connect(CacheBackend::Valkey, &format!("redis://127.0.0.1:{port}/0"))
RedisCacheStore::connect(CacheBackend::Valkey, &format!("redis://{host}:{port}/0"))
.await
.expect("runtime store must connect to Valkey"),
);