CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 12m45s
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
Community Release / release (push) Successful in 40m35s
Tool Draft фиксирует synchronous/background/adaptive, deadline и fallback; runtime применяет единый снимок политики (concurrency, очередь, approval, deadline) до внешнего действия. Насыщение отклоняется typed overload с retry guidance, mandatory safety store fail closed, background/adaptive используют опубликованный synchronous/unsupported fallback. Refs: #29
411 lines
16 KiB
Rust
411 lines
16 KiB
Rust
//! Preflight/legacy-state inspection (вынесено из `authority.rs`, чтобы файл
|
|
//! оставался в пределах rust-code-health); поведение не меняется.
|
|
|
|
use super::*;
|
|
|
|
pub(super) async fn inspect(
|
|
connection: &mut PgConnection,
|
|
) -> Result<MigrationPreflight, MigrationError> {
|
|
let core_exists = relation_exists(connection, "__crank_core_migrations").await?;
|
|
let canonical_exists = relation_exists(connection, "__crank_migrations").await?;
|
|
if !core_exists {
|
|
let mut owned_exists = canonical_exists;
|
|
for relation in OWNED_RELATIONS {
|
|
owned_exists |= relation_exists(connection, relation).await?;
|
|
}
|
|
if owned_exists {
|
|
if is_ledgerless_legacy_baseline(connection, canonical_exists).await? {
|
|
return Ok(MigrationPreflight::MigrationRequired {
|
|
current: 0,
|
|
target: CURRENT_VERSION,
|
|
});
|
|
}
|
|
return Err(MigrationError::new(
|
|
"partial_sequence",
|
|
"preflight.core_missing",
|
|
None,
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
inspect_optional_legacy(connection).await?;
|
|
return Ok(MigrationPreflight::MigrationRequired {
|
|
current: 0,
|
|
target: CURRENT_VERSION,
|
|
});
|
|
}
|
|
validate_core_ledger(connection).await?;
|
|
validate_required_relations(connection, owned_relations::BASELINE, 1).await?;
|
|
inspect_optional_legacy(connection).await?;
|
|
if !canonical_exists {
|
|
return Ok(MigrationPreflight::MigrationRequired {
|
|
current: 1,
|
|
target: CURRENT_VERSION,
|
|
});
|
|
}
|
|
let descriptors = MigrationAuthority::sequence();
|
|
let rows = query("select version, name, checksum, phase, compatibility from __crank_migrations order by version limit 1025")
|
|
.fetch_all(&mut *connection)
|
|
.await
|
|
.map_err(|_| MigrationError::storage("preflight.canonical"))?;
|
|
if rows.is_empty() {
|
|
return Err(MigrationError::new(
|
|
"partial_sequence",
|
|
"preflight.canonical",
|
|
None,
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
for (index, row) in rows.iter().enumerate() {
|
|
let version = row
|
|
.try_get::<i64, _>("version")
|
|
.map_err(|_| MigrationError::storage("preflight.canonical"))?;
|
|
if version > CURRENT_VERSION {
|
|
return Err(MigrationError::new(
|
|
"future_version",
|
|
"preflight.canonical",
|
|
Some(version),
|
|
"install_matching_application",
|
|
));
|
|
}
|
|
let Some(expected) = descriptors.get(index) else {
|
|
return Err(MigrationError::new(
|
|
"future_version",
|
|
"preflight.canonical",
|
|
Some(version),
|
|
"install_matching_application",
|
|
));
|
|
};
|
|
if version != expected.version {
|
|
return Err(MigrationError::new(
|
|
"partial_sequence",
|
|
"preflight.canonical",
|
|
Some(version),
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
let checksum = row
|
|
.try_get::<String, _>("checksum")
|
|
.map_err(|_| MigrationError::storage("preflight.canonical"))?;
|
|
if checksum != expected.checksum {
|
|
return Err(MigrationError::new(
|
|
"checksum_mismatch",
|
|
"preflight.canonical",
|
|
Some(version),
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
let name = row
|
|
.try_get::<String, _>("name")
|
|
.map_err(|_| MigrationError::storage("preflight.canonical"))?;
|
|
let phase = row
|
|
.try_get::<String, _>("phase")
|
|
.map_err(|_| MigrationError::storage("preflight.canonical"))?;
|
|
let compatibility = row
|
|
.try_get::<String, _>("compatibility")
|
|
.map_err(|_| MigrationError::storage("preflight.canonical"))?;
|
|
if name != expected.name
|
|
|| phase != expected.phase
|
|
|| compatibility != expected.compatibility
|
|
{
|
|
return Err(MigrationError::new(
|
|
"checksum_mismatch",
|
|
"preflight.metadata",
|
|
Some(version),
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
}
|
|
let current = rows
|
|
.last()
|
|
.and_then(|row| row.try_get::<i64, _>("version").ok())
|
|
.ok_or_else(|| MigrationError::storage("preflight.canonical"))?;
|
|
if rows.len() != usize::try_from(current).unwrap_or_default() {
|
|
return Err(MigrationError::new(
|
|
"partial_sequence",
|
|
"preflight.canonical",
|
|
Some(current),
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
validate_schema_fingerprint(connection, current).await?;
|
|
if current < CURRENT_VERSION {
|
|
Ok(MigrationPreflight::MigrationRequired {
|
|
current,
|
|
target: CURRENT_VERSION,
|
|
})
|
|
} else {
|
|
for relations in [
|
|
owned_relations::CONSOLIDATION,
|
|
owned_relations::ONBOARDING_PRODUCT_EVENTS,
|
|
owned_relations::ARTIFACT_METADATA,
|
|
owned_relations::IMPORT_PLAN,
|
|
owned_relations::ONBOARDING_IMPORT_SELECTION,
|
|
owned_relations::RESOURCE_DRAFTS,
|
|
owned_relations::RESOURCE_PUBLICATIONS,
|
|
owned_relations::AGENT_RESOURCE_BINDINGS,
|
|
owned_relations::RESOURCE_SECURITY_REVOCATIONS,
|
|
owned_relations::RESOURCE_TEMPLATE_SOURCES,
|
|
owned_relations::PROMPTS,
|
|
owned_relations::PROMPT_PUBLICATIONS,
|
|
owned_relations::PROMPT_DEPENDENCIES,
|
|
owned_relations::EXECUTION_ADMISSION,
|
|
] {
|
|
validate_required_relations(connection, relations, CURRENT_VERSION).await?;
|
|
}
|
|
validate_schema_fingerprint(connection, CURRENT_VERSION).await?;
|
|
validate_legacy_audit(connection).await?;
|
|
Ok(MigrationPreflight::Current { version: current })
|
|
}
|
|
}
|
|
pub(super) async fn is_ledgerless_legacy_baseline(
|
|
connection: &mut PgConnection,
|
|
canonical_exists: bool,
|
|
) -> Result<bool, MigrationError> {
|
|
if canonical_exists {
|
|
return Ok(false);
|
|
}
|
|
for relation in [
|
|
"__crank_migration_legacy_audit",
|
|
"master_key_identities",
|
|
"master_key_rotations",
|
|
"admin_bootstrap_contracts",
|
|
"admin_login_backoff",
|
|
"admin_security_audit_events",
|
|
"product_events",
|
|
"product_event_daily_rollups",
|
|
"onboarding_selections",
|
|
"onboarding_import_selections",
|
|
"artifact_blobs",
|
|
"artifact_sources",
|
|
] {
|
|
if relation_exists(connection, relation).await? {
|
|
return Ok(false);
|
|
}
|
|
}
|
|
for relation in owned_relations::BASELINE {
|
|
if !relation_exists(connection, relation).await? {
|
|
return Ok(false);
|
|
}
|
|
}
|
|
validate_required_relations(connection, owned_relations::BASELINE, 1).await?;
|
|
validate_ledgerless_baseline_fingerprint(connection).await?;
|
|
let mcp_ledger = relation_exists(connection, "__crank_mcp_migrations").await?;
|
|
if mcp_ledger {
|
|
return Ok(false);
|
|
}
|
|
let mcp_sessions = relation_exists(connection, "mcp_transport_sessions").await?;
|
|
let extension_ledger = relation_exists(connection, "__crank_ext_migrations").await?;
|
|
validate_ledgerless_optional_fingerprints(connection, mcp_sessions, extension_ledger).await?;
|
|
inspect_optional_legacy_for_ledgerless_baseline(connection).await?;
|
|
Ok(true)
|
|
}
|
|
pub(super) async fn validate_core_ledger(
|
|
connection: &mut PgConnection,
|
|
) -> Result<(), MigrationError> {
|
|
let rows = query("select version, description, checksum from __crank_core_migrations order by version limit 2")
|
|
.fetch_all(connection)
|
|
.await
|
|
.map_err(|_| MigrationError::storage("preflight.core"))?;
|
|
if rows.len() != 1 {
|
|
return Err(MigrationError::new(
|
|
"partial_sequence",
|
|
"preflight.core_cardinality",
|
|
None,
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
let version = rows[0]
|
|
.try_get::<i32, _>("version")
|
|
.map_err(|_| MigrationError::storage("preflight.core"))?;
|
|
let checksum = rows[0]
|
|
.try_get::<String, _>("checksum")
|
|
.map_err(|_| MigrationError::storage("preflight.core"))?;
|
|
let description = rows[0]
|
|
.try_get::<String, _>("description")
|
|
.map_err(|_| MigrationError::storage("preflight.core"))?;
|
|
if version != BASELINE_VERSION
|
|
|| description != "community baseline"
|
|
|| checksum != BASELINE_CHECKSUM
|
|
{
|
|
return Err(MigrationError::new(
|
|
"checksum_mismatch",
|
|
"preflight.core",
|
|
Some(i64::from(version)),
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
Ok(())
|
|
}
|
|
pub(super) async fn inspect_optional_legacy(
|
|
connection: &mut PgConnection,
|
|
) -> Result<(), MigrationError> {
|
|
inspect_optional_legacy_with_policy(connection, false).await
|
|
}
|
|
pub(super) async fn inspect_optional_legacy_for_ledgerless_baseline(
|
|
connection: &mut PgConnection,
|
|
) -> Result<(), MigrationError> {
|
|
inspect_optional_legacy_with_policy(connection, true).await
|
|
}
|
|
pub(super) async fn inspect_optional_legacy_with_policy(
|
|
connection: &mut PgConnection,
|
|
allow_sessions_without_ledger: bool,
|
|
) -> Result<(), MigrationError> {
|
|
let mcp_ledger = relation_exists(connection, "__crank_mcp_migrations").await?;
|
|
let mcp_sessions = relation_exists(connection, "mcp_transport_sessions").await?;
|
|
if mcp_ledger != mcp_sessions && !(allow_sessions_without_ledger && !mcp_ledger && mcp_sessions)
|
|
{
|
|
return Err(MigrationError::new(
|
|
"legacy_conflict",
|
|
"preflight.legacy_mcp",
|
|
None,
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
if mcp_ledger {
|
|
let rows =
|
|
query("select version, checksum from __crank_mcp_migrations order by version limit 2")
|
|
.fetch_all(&mut *connection)
|
|
.await
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_mcp"))?;
|
|
if rows.len() != 1
|
|
|| rows[0].try_get::<i32, _>("version").ok() != Some(1)
|
|
|| rows[0].try_get::<String, _>("checksum").ok().as_deref()
|
|
!= Some("mcp-transport-sessions-v1")
|
|
{
|
|
return Err(MigrationError::new(
|
|
"legacy_conflict",
|
|
"preflight.legacy_mcp",
|
|
None,
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
}
|
|
if relation_exists(connection, "__crank_ext_migrations").await? {
|
|
let count = query("select count(*)::bigint as count from __crank_ext_migrations")
|
|
.fetch_one(&mut *connection)
|
|
.await
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_extension"))?
|
|
.try_get::<i64, _>("count")
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_extension"))?;
|
|
if count > 1_000 {
|
|
return Err(MigrationError::new(
|
|
"legacy_conflict",
|
|
"preflight.legacy_extension",
|
|
None,
|
|
"contact_operator",
|
|
));
|
|
}
|
|
if count > 0 {
|
|
let checksum_column = query(
|
|
"select exists (
|
|
select 1 from information_schema.columns
|
|
where table_schema = current_schema()
|
|
and table_name = '__crank_ext_migrations'
|
|
and column_name = 'checksum'
|
|
) as present",
|
|
)
|
|
.fetch_one(&mut *connection)
|
|
.await
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_extension"))?
|
|
.try_get::<bool, _>("present")
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_extension"))?;
|
|
if !checksum_column {
|
|
return Err(MigrationError::new(
|
|
"legacy_conflict",
|
|
"preflight.legacy_extension",
|
|
None,
|
|
"contact_operator",
|
|
));
|
|
}
|
|
let rows = query(
|
|
"select extension_name, version, checksum
|
|
from __crank_ext_migrations
|
|
order by extension_name, version
|
|
limit 1001",
|
|
)
|
|
.fetch_all(&mut *connection)
|
|
.await
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_extension"))?;
|
|
for row in rows {
|
|
let name = row
|
|
.try_get::<String, _>("extension_name")
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_extension"))?;
|
|
let version = row
|
|
.try_get::<i32, _>("version")
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_extension"))?;
|
|
let checksum = row
|
|
.try_get::<Option<String>, _>("checksum")
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_extension"))?;
|
|
let registered = MigrationAuthority::registered_extension_migrations()
|
|
.iter()
|
|
.any(|(registered_name, descriptor)| {
|
|
*registered_name == name
|
|
&& descriptor.version == u32::try_from(version).unwrap_or_default()
|
|
&& checksum.as_deref() == Some(descriptor.checksum)
|
|
});
|
|
if !registered {
|
|
return Err(MigrationError::new(
|
|
"legacy_conflict",
|
|
"preflight.legacy_extension",
|
|
Some(i64::from(version)),
|
|
"contact_operator",
|
|
));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
pub(super) async fn validate_legacy_audit(
|
|
connection: &mut PgConnection,
|
|
) -> Result<(), MigrationError> {
|
|
let rows = query(
|
|
"select source, source_version, source_checksum
|
|
from __crank_migration_legacy_audit
|
|
order by source, source_version
|
|
limit 1002",
|
|
)
|
|
.fetch_all(&mut *connection)
|
|
.await
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_audit"))?;
|
|
if rows.len() > 1001 {
|
|
return Err(MigrationError::new(
|
|
"legacy_conflict",
|
|
"preflight.legacy_audit",
|
|
None,
|
|
"contact_operator",
|
|
));
|
|
}
|
|
for row in rows {
|
|
let source = row
|
|
.try_get::<String, _>("source")
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_audit"))?;
|
|
let version = row
|
|
.try_get::<i64, _>("source_version")
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_audit"))?;
|
|
let checksum = row
|
|
.try_get::<String, _>("source_checksum")
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_audit"))?;
|
|
let valid = (source == "core" && version == 1 && checksum == BASELINE_CHECKSUM)
|
|
|| (source == "mcp-session" && version == 1 && checksum == "mcp-transport-sessions-v1")
|
|
|| source.strip_prefix("extension:").is_some_and(|name| {
|
|
MigrationAuthority::registered_extension_migrations()
|
|
.iter()
|
|
.any(|(registered_name, descriptor)| {
|
|
*registered_name == name
|
|
&& i64::from(descriptor.version) == version
|
|
&& descriptor.checksum == checksum
|
|
})
|
|
});
|
|
if !valid {
|
|
return Err(MigrationError::new(
|
|
"legacy_conflict",
|
|
"preflight.legacy_audit",
|
|
Some(version),
|
|
"restore_known_good_backup",
|
|
));
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|