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.
312 lines
12 KiB
Rust
312 lines
12 KiB
Rust
//! BUILD-ONLY PG Story 3.10 (FR-16): version-keyed template definitions (V20).
|
|
//!
|
|
//! Проверяет то, что видно только на реальной схеме: definition-строки
|
|
//! insert-only, публикация ссылается на ту же version-keyed строку, правка Draft
|
|
//! не меняет опубликованное определение, CAS-гарды не создают дублей/сирот, а
|
|
//! artifact-строки сохраняют прежние инварианты.
|
|
|
|
use sha2::{Digest, Sha256};
|
|
use sqlx::Row as _;
|
|
use time::OffsetDateTime;
|
|
|
|
use crank_core::ids::ResourceId;
|
|
use crank_registry::model::resource::{
|
|
CreateResourceTemplateDraftRequest, PublishResourceTemplateVersionRequest,
|
|
ResourceTemplateDraftRecord, UpdateResourceTemplateDraftRequest,
|
|
};
|
|
|
|
use super::*;
|
|
use crank_registry::PostgresRegistry;
|
|
|
|
const WORKSPACE: &str = WORKSPACE_ID;
|
|
|
|
fn sha256_hex(value: &str) -> String {
|
|
format!("{:x}", Sha256::digest(value.as_bytes()))
|
|
}
|
|
|
|
fn definition(operation_id: &str, uri_template: &str) -> String {
|
|
json!({
|
|
"operation_id": operation_id,
|
|
"operation_version": 1,
|
|
"uri_template": uri_template,
|
|
"parameters": [{
|
|
"name": "order_id",
|
|
"input_path": "input.order_id",
|
|
"schema": { "type": "string", "required": true, "format": "uuid" }
|
|
}],
|
|
"cache": { "ttl_ms": 60000, "vary": ["order_id"], "max_bytes": 65536 },
|
|
"failure": { "outcome": "error" }
|
|
})
|
|
.to_string()
|
|
}
|
|
|
|
async fn create_template(
|
|
registry: &PostgresRegistry,
|
|
resource_id: &ResourceId,
|
|
uri: &str,
|
|
name: &str,
|
|
definition_json: &str,
|
|
) -> ResourceTemplateDraftRecord {
|
|
registry
|
|
.create_resource_template_draft(CreateResourceTemplateDraftRequest {
|
|
workspace_id: WORKSPACE.into(),
|
|
resource_id: resource_id.clone(),
|
|
uri: uri.to_owned(),
|
|
name: name.to_owned(),
|
|
description: String::new(),
|
|
kind: "text".to_owned(),
|
|
mime_type: "text/plain".to_owned(),
|
|
sensitivity: "internal".to_owned(),
|
|
definition_json: definition_json.to_owned(),
|
|
definition_digest: sha256_hex(definition_json),
|
|
created_at: OffsetDateTime::now_utc(),
|
|
})
|
|
.await
|
|
.expect("template draft must be created")
|
|
}
|
|
|
|
async fn publish_template(
|
|
registry: &PostgresRegistry,
|
|
resource_id: &ResourceId,
|
|
record: &ResourceTemplateDraftRecord,
|
|
) -> Result<(), crank_core::resource::ResourceError> {
|
|
registry
|
|
.publish_resource_template_version(PublishResourceTemplateVersionRequest {
|
|
workspace_id: WORKSPACE.into(),
|
|
resource_id: resource_id.clone(),
|
|
expected_updated_at: record.snapshot.resource.updated_at,
|
|
expected_version: record.snapshot.version.version,
|
|
expected_digest: record.source.definition_digest.clone(),
|
|
published_at: OffsetDateTime::now_utc(),
|
|
})
|
|
.await
|
|
.map(|_| ())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn story310_at06_template_source_is_immutable_in_postgres() {
|
|
let sandbox = sandbox("story310_template_immutable").await;
|
|
let registry = &sandbox.registry;
|
|
let resource_id = ResourceId::new("res_story310_template_immutable");
|
|
let first = definition("op_story310_first", "crank://orders/{order_id}");
|
|
let created = create_template(
|
|
registry,
|
|
&resource_id,
|
|
"crank://story310/immutable",
|
|
"story310_immutable",
|
|
&first,
|
|
)
|
|
.await;
|
|
assert_eq!(created.snapshot.version.source_kind, "template");
|
|
assert_eq!(created.snapshot.version.size_bytes, 0);
|
|
assert_eq!(created.source.definition_json, first);
|
|
|
|
// Definition-строка insert-only: UPDATE отвергается trigger-ом БД.
|
|
let tamper = sqlx::query(
|
|
"update resource_template_sources set definition_json = $1
|
|
where workspace_id = $2 and resource_id = $3 and version = 1",
|
|
)
|
|
.bind(definition("op_story310_other", "crank://orders/{order_id}"))
|
|
.bind(WORKSPACE)
|
|
.bind(resource_id.as_str())
|
|
.execute(registry.pool())
|
|
.await;
|
|
assert!(
|
|
tamper.is_err(),
|
|
"a stored template definition must not be mutable"
|
|
);
|
|
|
|
publish_template(registry, &resource_id, &created)
|
|
.await
|
|
.expect("the draft must publish");
|
|
let (publication, source) = registry
|
|
.resolve_resource_template_publication(WORKSPACE, &resource_id, 1)
|
|
.await
|
|
.expect("published template snapshot must resolve");
|
|
assert_eq!(publication.source_kind, "template");
|
|
assert_eq!(publication.size_bytes, 0);
|
|
assert_eq!(source.definition_json, first);
|
|
assert_eq!(source.definition_digest, sha256_hex(&first));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn story310_at06_draft_edit_does_not_mutate_published_source() {
|
|
let sandbox = sandbox("story310_template_edit").await;
|
|
let registry = &sandbox.registry;
|
|
let resource_id = ResourceId::new("res_story310_template_edit");
|
|
let first = definition("op_story310_first", "crank://orders/{order_id}");
|
|
let second = definition("op_story310_second", "crank://orders/{order_id}");
|
|
let created = create_template(
|
|
registry,
|
|
&resource_id,
|
|
"crank://story310/edit",
|
|
"story310_edit",
|
|
&first,
|
|
)
|
|
.await;
|
|
publish_template(registry, &resource_id, &created)
|
|
.await
|
|
.expect("first publication must succeed");
|
|
// Публикация сдвигает CAS-ревизию карточки: редактирование берёт её из свежего чтения.
|
|
let published_draft = registry
|
|
.get_resource_draft(WORKSPACE, &resource_id)
|
|
.await
|
|
.expect("published draft card must be readable");
|
|
|
|
let updated = registry
|
|
.update_resource_template_draft(UpdateResourceTemplateDraftRequest {
|
|
workspace_id: WORKSPACE.into(),
|
|
resource_id: resource_id.clone(),
|
|
description: "retargeted".to_owned(),
|
|
mime_type: "text/plain".to_owned(),
|
|
sensitivity: "internal".to_owned(),
|
|
expected_updated_at: published_draft.resource.updated_at,
|
|
definition_json: second.clone(),
|
|
definition_digest: sha256_hex(&second),
|
|
})
|
|
.await
|
|
.expect("draft edit must create the next version");
|
|
assert_eq!(updated.snapshot.version.version, 2);
|
|
assert_eq!(updated.source.definition_json, second);
|
|
|
|
// Published v1 остаётся прежним source-снимком.
|
|
let (_, published_source) = registry
|
|
.resolve_resource_template_publication(WORKSPACE, &resource_id, 1)
|
|
.await
|
|
.expect("published v1 must stay readable");
|
|
assert_eq!(published_source.definition_json, first);
|
|
|
|
// Устаревший precondition публикации не создаёт новую версию и не пишет сирот.
|
|
let stale = registry
|
|
.publish_resource_template_version(PublishResourceTemplateVersionRequest {
|
|
workspace_id: WORKSPACE.into(),
|
|
resource_id: resource_id.clone(),
|
|
expected_updated_at: created.snapshot.resource.updated_at,
|
|
expected_version: created.snapshot.version.version,
|
|
expected_digest: created.source.definition_digest.clone(),
|
|
published_at: OffsetDateTime::now_utc(),
|
|
})
|
|
.await;
|
|
assert_eq!(stale, Err(crank_core::resource::ResourceError::Stale));
|
|
let publications: i64 = sqlx::query(
|
|
"select count(*) as count from resource_publications
|
|
where workspace_id = $1 and resource_id = $2",
|
|
)
|
|
.bind(WORKSPACE)
|
|
.bind(resource_id.as_str())
|
|
.fetch_one(registry.pool())
|
|
.await
|
|
.expect("publication count must be readable")
|
|
.try_get("count")
|
|
.expect("count column");
|
|
assert_eq!(publications, 1, "a stale publish must not add a row");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn story310_at06_concurrent_publish_uses_the_existing_guard() {
|
|
let sandbox = sandbox("story310_template_concurrent").await;
|
|
let registry = &sandbox.registry;
|
|
let resource_id = ResourceId::new("res_story310_template_concurrent");
|
|
let created = create_template(
|
|
registry,
|
|
&resource_id,
|
|
"crank://story310/concurrent",
|
|
"story310_concurrent",
|
|
&definition("op_story310_first", "crank://orders/{order_id}"),
|
|
)
|
|
.await;
|
|
let request = || PublishResourceTemplateVersionRequest {
|
|
workspace_id: WORKSPACE.into(),
|
|
resource_id: resource_id.clone(),
|
|
expected_updated_at: created.snapshot.resource.updated_at,
|
|
expected_version: created.snapshot.version.version,
|
|
expected_digest: created.source.definition_digest.clone(),
|
|
published_at: OffsetDateTime::now_utc(),
|
|
};
|
|
// Один и тот же precondition: row-lock + CAS пропускают ровно одну публикацию.
|
|
let (first, second) = tokio::join!(
|
|
registry.publish_resource_template_version(request()),
|
|
registry.publish_resource_template_version(request()),
|
|
);
|
|
let successes = usize::from(first.is_ok()) + usize::from(second.is_ok());
|
|
assert_eq!(successes, 1, "exactly one concurrent publish may win");
|
|
let stale = [first, second]
|
|
.into_iter()
|
|
.filter_map(Result::err)
|
|
.collect::<Vec<_>>();
|
|
assert_eq!(stale, vec![crank_core::resource::ResourceError::Stale]);
|
|
let publications: i64 = sqlx::query(
|
|
"select count(*) as count from resource_publications
|
|
where workspace_id = $1 and resource_id = $2",
|
|
)
|
|
.bind(WORKSPACE)
|
|
.bind(resource_id.as_str())
|
|
.fetch_one(registry.pool())
|
|
.await
|
|
.expect("publication count must be readable")
|
|
.try_get("count")
|
|
.expect("count column");
|
|
assert_eq!(publications, 1);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn story310_at06_failed_template_create_leaves_no_orphan_and_static_rows_keep_checks() {
|
|
let sandbox = sandbox("story310_template_orphan").await;
|
|
let registry = &sandbox.registry;
|
|
// Static Draft по-прежнему создаётся и остаётся artifact-строкой.
|
|
let static_id = seeded_draft(&sandbox).await;
|
|
let static_record = registry
|
|
.get_resource_draft(WORKSPACE, &static_id)
|
|
.await
|
|
.expect("static draft must stay readable");
|
|
assert_eq!(static_record.version.source_kind, "artifact");
|
|
assert!(static_record.version.size_bytes > 0);
|
|
|
|
// Дубликат URI: транзакция create шаблона обязана откатиться целиком.
|
|
let resource_id = ResourceId::new("res_story310_template_orphan");
|
|
let definition_json = definition("op_story310_first", "crank://orders/{order_id}");
|
|
let duplicate = registry
|
|
.create_resource_template_draft(CreateResourceTemplateDraftRequest {
|
|
workspace_id: WORKSPACE.into(),
|
|
resource_id: resource_id.clone(),
|
|
uri: static_record.resource.uri.clone(),
|
|
name: "story310_orphan".to_owned(),
|
|
description: String::new(),
|
|
kind: "text".to_owned(),
|
|
mime_type: "text/plain".to_owned(),
|
|
sensitivity: "internal".to_owned(),
|
|
definition_json: definition_json.clone(),
|
|
definition_digest: sha256_hex(&definition_json),
|
|
created_at: OffsetDateTime::now_utc(),
|
|
})
|
|
.await;
|
|
assert_eq!(
|
|
duplicate,
|
|
Err(crank_core::resource::ResourceError::UriDuplicate)
|
|
);
|
|
let orphans: i64 = sqlx::query(
|
|
"select count(*) as count from resource_template_sources
|
|
where workspace_id = $1 and resource_id = $2",
|
|
)
|
|
.bind(WORKSPACE)
|
|
.bind(resource_id.as_str())
|
|
.fetch_one(registry.pool())
|
|
.await
|
|
.expect("template source count must be readable")
|
|
.try_get("count")
|
|
.expect("count column");
|
|
assert_eq!(orphans, 0, "a rejected create must not leave a definition");
|
|
let versions: i64 = sqlx::query(
|
|
"select count(*) as count from resource_versions
|
|
where workspace_id = $1 and resource_id = $2",
|
|
)
|
|
.bind(WORKSPACE)
|
|
.bind(resource_id.as_str())
|
|
.fetch_one(registry.pool())
|
|
.await
|
|
.expect("version count must be readable")
|
|
.try_get("count")
|
|
.expect("count column");
|
|
assert_eq!(versions, 0, "a rejected create must not leave a version");
|
|
}
|