CI / Rust Checks (pull_request) Failing after 13m41s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
Community Release / release (push) Failing after 13m33s
Recovery delivery of the accumulated, TL-accepted Epic 3 work that had never been
committed to Git (stories 3.6-3.17, see the crank board). Story 3.18 is not accepted
yet and is deliberately excluded: its logging surface, its capability registration,
its oracle projections and its tests are not part of this tree.
Accepted scope
- 3.6 typed static Resource Drafts; 3.7 publication and exact binding of static
Resource Versions; 3.8 MCP discovery/read and revocation of static Resources;
- 3.9 Resource import/export in API and UI plus TLS dependency maintenance;
3.10 REST-backed Resource templates; 3.11 shared REST read runtime and the MCP
revision/capability foundation;
- 3.12 parameterised Prompt Drafts with preview; 3.13 Prompt publication and exact
version binding to Agents; 3.14 MCP prompts; 3.15 Prompt import/export;
- 3.16 composite Prompts with published Resources (dependency content scan plus
dynamic read through the accepted 3.11 runtime, admin preview and MCP prompts/get);
- 3.17 bounded MCP completion suggestions from typed published schemas and the
resources/list admission fix for bound template publications.
Accepted evidence being reused (unchanged code paths)
- 3.16 candidate: core 7, admin-api 22, registry 4, mcp-server 4, UI 6; one final
`just verify` run 1651 passed / 0 failed / 2 ignored.
- 3.17 candidate: full run 1672 passed / 0 failed / 2 ignored, QA targeted reruns and
a real MCP Inspector 2.12.0 client smoke.
Delivery engineering in this commit
- CI: one event per change (feature branches are gated by their pull request, push
runs only on main), story events run the changed packages, the full workspace suite
stays the feature/release gate; no cache/sharding without a measured runner check.
- Release: the packaged archives, checksums and SBOM are now uploaded to the Gitea
release entry of the tag instead of dying with the runner workspace.
Not included on purpose: Story 3.18 work, build caches, runtime databases, local
credentials and scratch evidence.
Refs: crank kanban t_e39d7263; baseline 1b6db09bd1.
390 lines
18 KiB
Rust
390 lines
18 KiB
Rust
use super::*;
|
|
use base64::Engine as _;
|
|
|
|
impl ResourcesService {
|
|
async fn test_resource_read(
|
|
&self,
|
|
admin: &super::super::AdminService,
|
|
workspace_id: &WorkspaceId,
|
|
resource_id: &ResourceId,
|
|
body: Value,
|
|
correlation: &crank_core::CorrelationContext,
|
|
) -> Result<Value, ApiError> {
|
|
let object = body
|
|
.as_object()
|
|
.ok_or_else(|| ApiError::validation("invalid resource test request"))?;
|
|
if object.len() != 4
|
|
|| !["version", "digest", "updated_at", "parameters"]
|
|
.iter()
|
|
.all(|key| object.contains_key(*key))
|
|
{
|
|
return Err(ApiError::validation("invalid resource test request"));
|
|
}
|
|
let version = object
|
|
.get("version")
|
|
.and_then(Value::as_u64)
|
|
.and_then(|v| u32::try_from(v).ok())
|
|
.ok_or_else(|| ApiError::validation("invalid resource test request"))?;
|
|
let digest = object
|
|
.get("digest")
|
|
.and_then(Value::as_str)
|
|
.ok_or_else(|| ApiError::validation("invalid resource test request"))?;
|
|
let updated_at = object
|
|
.get("updated_at")
|
|
.and_then(Value::as_str)
|
|
.ok_or_else(|| ApiError::validation("invalid resource test request"))?;
|
|
let values = object
|
|
.get("parameters")
|
|
.and_then(Value::as_object)
|
|
.ok_or_else(|| ApiError::validation("invalid resource test request"))?;
|
|
|
|
let current = admin.resources.card(workspace_id, resource_id).await?;
|
|
if current.resource.updated_at != updated_at
|
|
|| current.version.version != version
|
|
|| current.version.digest != digest
|
|
{
|
|
return Err(ApiError::conflict_with_context(
|
|
"Resource changed; reload before testing",
|
|
json!({"error_code":"resource_stale","recovery":"reload"}),
|
|
));
|
|
}
|
|
if current.resource.status == crank_core::resource::ResourceStatus::Archived {
|
|
return Err(
|
|
crank_core::resource::ResourceError::InvalidLifecycleTransition {
|
|
from: current.resource.status,
|
|
to: crank_core::resource::ResourceStatus::Draft,
|
|
}
|
|
.into(),
|
|
);
|
|
}
|
|
let stored = admin
|
|
.registry
|
|
.get_resource_template_source(workspace_id.as_str(), resource_id, version)
|
|
.await?;
|
|
let definition = template::TemplateDefinition::from_stored(&stored)?;
|
|
let source = &definition.source;
|
|
let operation_id: crank_core::OperationId = source.operation_id.as_str().into();
|
|
let operation = admin
|
|
.registry
|
|
.get_operation_version(workspace_id, &operation_id, source.operation_version)
|
|
.await?
|
|
.ok_or(crank_core::resource::ResourceError::SourceNotFound)?;
|
|
if operation.status != crank_core::operation::OperationStatus::Published {
|
|
return Err(crank_core::resource::ResourceError::SourceNotPublished.into());
|
|
}
|
|
let operation_detail = admin.get_operation(workspace_id, &operation_id).await?;
|
|
if operation_detail.status == crank_core::operation::OperationStatus::Archived {
|
|
return Err(crank_core::resource::ResourceError::SourceNotPublished.into());
|
|
}
|
|
let findings = template::template_findings(&definition, &operation);
|
|
if !findings.is_empty() {
|
|
return Err(crank_core::resource::ResourceError::SourceBlocked { findings }.into());
|
|
}
|
|
if values.keys().any(|name| {
|
|
!source
|
|
.parameters
|
|
.iter()
|
|
.any(|parameter| ¶meter.name == name)
|
|
}) {
|
|
return Err(ApiError::validation(
|
|
"resource parameters do not match the saved definition",
|
|
));
|
|
}
|
|
let mut input = serde_json::Map::new();
|
|
for (parameter, schema) in source.parameters.iter().zip(&definition.schemas) {
|
|
let value = values
|
|
.get(¶meter.name)
|
|
.or(schema.default_value.as_ref())
|
|
.ok_or_else(|| {
|
|
ApiError::validation("resource parameters do not match the saved definition")
|
|
})?;
|
|
let parameter_schema: crank_schema::Schema =
|
|
serde_json::from_value(parameter.schema.clone()).map_err(|_| {
|
|
ApiError::validation("saved resource parameter schema is invalid")
|
|
})?;
|
|
crank_runtime::validate_resource_template_parameter(¶meter_schema, value).map_err(
|
|
|_| ApiError::validation("resource parameter does not match the saved definition"),
|
|
)?;
|
|
// Validate the narrower Resource contract before assembling input; the runtime
|
|
// separately validates the pinned Operation contract before outbound dispatch.
|
|
if !set_resource_input_path(&mut input, ¶meter.input_path, value.clone()) {
|
|
return Err(ApiError::validation(
|
|
"saved resource parameter mapping is invalid",
|
|
));
|
|
}
|
|
}
|
|
let runtime = crank_runtime::RuntimeOperation::from(operation.snapshot.clone());
|
|
let context = crank_runtime::RuntimeRequestContext::from_correlation(correlation)
|
|
.with_metering_context(
|
|
workspace_id.clone(),
|
|
None,
|
|
crank_core::InvocationSource::AdminTestRun,
|
|
);
|
|
let request_id = context.request_id.as_str().to_owned();
|
|
let trace_id = context.trace_context.trace_id().to_string();
|
|
let auth = admin
|
|
.resolve_operation_auth(workspace_id, &runtime.execution_config, None)
|
|
.await
|
|
.map_err(|_| {
|
|
ApiError::unprocessable_with_context(
|
|
"operation authorization is unavailable",
|
|
json!({"error_code":"resource_test_authorization_failed"}),
|
|
)
|
|
})?;
|
|
let deadline = std::time::Instant::now()
|
|
+ std::time::Duration::from_millis(runtime.execution_config.timeout_ms.max(1));
|
|
let input = Value::Object(input);
|
|
let resource_guard = admin
|
|
.registry
|
|
.begin_authorized_resource_test_read(
|
|
workspace_id.as_str(),
|
|
resource_id,
|
|
version,
|
|
digest,
|
|
)
|
|
.await?;
|
|
let execution = crank_runtime::RuntimeExecutionRequest::try_new(
|
|
workspace_id,
|
|
crank_core::ExecutionOrigin::AdminDraft,
|
|
None,
|
|
&runtime,
|
|
&input,
|
|
crank_runtime::ExecutionAuthorization::Authorized,
|
|
auth.as_ref(),
|
|
&context,
|
|
deadline,
|
|
)
|
|
.map_err(|_| ApiError::validation("resource test request is invalid"))?;
|
|
let started_at = std::time::Instant::now();
|
|
let execution_result = admin.runtime.execute_outcome(execution).await;
|
|
resource_guard.commit().await?;
|
|
let duration_ms = u64::try_from(started_at.elapsed().as_millis()).unwrap_or(u64::MAX);
|
|
match execution_result {
|
|
Ok(success) => {
|
|
let kind = crank_core::resource::ResourceKind::parse(¤t.resource.kind)
|
|
.ok_or_else(|| ApiError::unprocessable_with_context("saved Resource kind is invalid", json!({"error_code":"resource_test_output_invalid","request_id":request_id,"trace_id":trace_id})))?;
|
|
if !kind.allows_mime_type(¤t.version.mime_type) {
|
|
return Err(ApiError::unprocessable_with_context(
|
|
"saved Resource representation is invalid",
|
|
json!({"error_code":"resource_test_output_invalid","request_id":request_id,"trace_id":trace_id}),
|
|
));
|
|
}
|
|
let representation = match kind {
|
|
crank_core::resource::ResourceKind::Text => success.output.as_str().map(str::as_bytes).map(Vec::from),
|
|
crank_core::resource::ResourceKind::Json if !success.output.is_null() => serde_json::to_vec(&success.output).ok(),
|
|
crank_core::resource::ResourceKind::Binary => success
|
|
.output
|
|
.as_object()
|
|
.filter(|object| object.len() == 1 && object.contains_key("data_base64"))
|
|
.and_then(|object| object.get("data_base64"))
|
|
.and_then(Value::as_str)
|
|
.and_then(|encoded| base64::engine::general_purpose::STANDARD.decode(encoded).ok()),
|
|
_ => None,
|
|
}.filter(|bytes| !bytes.is_empty() && bytes.len() as u64 <= source.cache.max_bytes)
|
|
.ok_or_else(|| ApiError::unprocessable_with_context(
|
|
"test read output does not match the saved Resource contract",
|
|
json!({"error_code":"resource_test_output_invalid","request_id":request_id,"trace_id":trace_id}),
|
|
))?;
|
|
let _representation = representation;
|
|
let response_preview = crank_core::sanitize_invocation_preview(&success.output);
|
|
admin
|
|
.record_invocation(super::super::InvocationRecordRequest {
|
|
workspace_id,
|
|
agent_id: None,
|
|
operation: &operation.snapshot,
|
|
request_id: Some(&request_id),
|
|
trace_id: Some(&trace_id),
|
|
source: crank_core::InvocationSource::AdminTestRun,
|
|
level: crank_core::InvocationLevel::Info,
|
|
status: crank_core::InvocationStatus::Ok,
|
|
message: "administrative Resource test read completed".to_owned(),
|
|
status_code: None,
|
|
error_kind: None,
|
|
execution_stage: Some(crank_core::ExecutionStage::Runtime),
|
|
execution_error_code: None,
|
|
retryability: Some(crank_core::Retryability::Never),
|
|
outcome_certainty: Some(crank_core::OutcomeCertainty::Certain),
|
|
duration_ms,
|
|
request_preview: success.request_preview.clone(),
|
|
response_preview: response_preview.clone(),
|
|
})
|
|
.await;
|
|
Ok(
|
|
json!({"ok":true,"request_id":request_id,"trace_id":trace_id,"output":response_preview}),
|
|
)
|
|
}
|
|
Err(failure) => {
|
|
admin
|
|
.record_invocation(super::super::InvocationRecordRequest {
|
|
workspace_id,
|
|
agent_id: None,
|
|
operation: &operation.snapshot,
|
|
request_id: Some(&request_id),
|
|
trace_id: Some(&trace_id),
|
|
source: crank_core::InvocationSource::AdminTestRun,
|
|
level: crank_core::InvocationLevel::Error,
|
|
status: crank_core::InvocationStatus::Error,
|
|
message: "administrative Resource test read failed".to_owned(),
|
|
status_code: failure.upstream_status(),
|
|
error_kind: Some(failure.error_code().as_str().to_owned()),
|
|
execution_stage: Some(failure.stage()),
|
|
execution_error_code: Some(failure.error_code()),
|
|
retryability: Some(failure.retryability()),
|
|
outcome_certainty: Some(failure.outcome_certainty()),
|
|
duration_ms,
|
|
request_preview: Value::Null,
|
|
response_preview: Value::Null,
|
|
})
|
|
.await;
|
|
Ok(
|
|
json!({"ok":false,"request_id":request_id,"trace_id":trace_id,"error":crate::error::execution_test_failure_localized(&failure, crank_core::ExecutionLocale::En)}),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn execute_template_test(
|
|
&self,
|
|
admin: &super::super::AdminService,
|
|
workspace_id: &WorkspaceId,
|
|
resource_id: &ResourceId,
|
|
body: Value,
|
|
correlation: &crank_core::CorrelationContext,
|
|
) -> Result<Value, ApiError> {
|
|
self.test_resource_read(admin, workspace_id, resource_id, body, correlation)
|
|
.await
|
|
}
|
|
|
|
/// Story 3.16 §4: pinned content of a dynamic (template) dependency.
|
|
///
|
|
/// The Prompt dependency names only the exact `(resource_id, version)` of the template, so the
|
|
/// declared read Operation is executed through the same 3.11 runtime (authorization, outbound,
|
|
/// size and live revocation gate) with the template's own parameter defaults. A parameter
|
|
/// without a default cannot be supplied by a Prompt, and every failure stays `None` — the
|
|
/// caller turns that into `prompt_dependency_unavailable` instead of substituting definition
|
|
/// text or stale content.
|
|
pub(crate) async fn read_pinned_template_content(
|
|
&self,
|
|
admin: &super::super::AdminService,
|
|
workspace_id: &WorkspaceId,
|
|
resource_id: &ResourceId,
|
|
version: u32,
|
|
correlation: &crank_core::CorrelationContext,
|
|
) -> Option<Vec<u8>> {
|
|
let (publication, stored) = admin
|
|
.registry
|
|
.resolve_resource_template_publication(workspace_id.as_str(), resource_id, version)
|
|
.await
|
|
.ok()?;
|
|
let kind = crank_core::resource::ResourceKind::parse(&publication.kind)?;
|
|
if !kind.allows_mime_type(&publication.mime_type) {
|
|
return None;
|
|
}
|
|
let definition = template::TemplateDefinition::from_stored(&stored).ok()?;
|
|
let source = &definition.source;
|
|
let operation_id: crank_core::OperationId = source.operation_id.as_str().into();
|
|
let operation = admin
|
|
.registry
|
|
.get_operation_version(workspace_id, &operation_id, source.operation_version)
|
|
.await
|
|
.ok()??;
|
|
if operation.status != crank_core::operation::OperationStatus::Published {
|
|
return None;
|
|
}
|
|
let detail = admin
|
|
.get_operation(workspace_id, &operation_id)
|
|
.await
|
|
.ok()?;
|
|
if detail.status == crank_core::operation::OperationStatus::Archived {
|
|
return None;
|
|
}
|
|
if !template::template_findings(&definition, &operation).is_empty() {
|
|
return None;
|
|
}
|
|
let mut input = serde_json::Map::new();
|
|
for (parameter, schema) in source.parameters.iter().zip(&definition.schemas) {
|
|
let value = schema.default_value.as_ref()?;
|
|
let parameter_schema: crank_schema::Schema =
|
|
serde_json::from_value(parameter.schema.clone()).ok()?;
|
|
crank_runtime::validate_resource_template_parameter(¶meter_schema, value).ok()?;
|
|
if !set_resource_input_path(&mut input, ¶meter.input_path, value.clone()) {
|
|
return None;
|
|
}
|
|
}
|
|
let runtime = crank_runtime::RuntimeOperation::from(operation.snapshot.clone());
|
|
let context = crank_runtime::RuntimeRequestContext::from_correlation(correlation)
|
|
.with_metering_context(
|
|
workspace_id.clone(),
|
|
None,
|
|
crank_core::InvocationSource::AdminTestRun,
|
|
);
|
|
let auth = admin
|
|
.resolve_operation_auth(workspace_id, &runtime.execution_config, None)
|
|
.await
|
|
.ok()?;
|
|
let deadline = std::time::Instant::now()
|
|
+ std::time::Duration::from_millis(runtime.execution_config.timeout_ms.max(1));
|
|
let input = Value::Object(input);
|
|
let guard = admin
|
|
.registry
|
|
.begin_authorized_resource_template_read(
|
|
workspace_id.as_str(),
|
|
resource_id,
|
|
publication.version,
|
|
)
|
|
.await
|
|
.ok()?;
|
|
let request = crank_runtime::RuntimeExecutionRequest::try_new(
|
|
workspace_id,
|
|
crank_core::ExecutionOrigin::AdminDraft,
|
|
None,
|
|
&runtime,
|
|
&input,
|
|
crank_runtime::ExecutionAuthorization::Authorized,
|
|
auth.as_ref(),
|
|
&context,
|
|
deadline,
|
|
)
|
|
.ok()?;
|
|
let success = admin.runtime.execute_outcome(request).await.ok()?;
|
|
guard.commit().await.ok()?;
|
|
let max_bytes = usize::try_from(source.cache.max_bytes).unwrap_or(usize::MAX);
|
|
match kind {
|
|
crank_core::resource::ResourceKind::Text => {
|
|
success.output.as_str().map(str::as_bytes).map(Vec::from)
|
|
}
|
|
crank_core::resource::ResourceKind::Json if !success.output.is_null() => {
|
|
serde_json::to_vec(&success.output).ok()
|
|
}
|
|
_ => None,
|
|
}
|
|
.filter(|bytes| !bytes.is_empty() && bytes.len() <= max_bytes)
|
|
}
|
|
}
|
|
|
|
fn set_resource_input_path(
|
|
root: &mut serde_json::Map<String, Value>,
|
|
path: &str,
|
|
value: Value,
|
|
) -> bool {
|
|
let Some((prefix, tail)) = path.split_once('.') else {
|
|
return false;
|
|
};
|
|
if prefix != "input" {
|
|
return false;
|
|
}
|
|
let mut current = root;
|
|
let mut parts = tail.split('.').peekable();
|
|
while let Some(part) = parts.next() {
|
|
if parts.peek().is_none() {
|
|
return current.insert(part.to_owned(), value).is_none();
|
|
}
|
|
let entry = current.entry(part.to_owned()).or_insert_with(|| json!({}));
|
|
let Some(object) = entry.as_object_mut() else {
|
|
return false;
|
|
};
|
|
current = object;
|
|
}
|
|
false
|
|
}
|