feat: complete Epic 1 production foundation

This commit is contained in:
bsodfather committed 2026-08-25 01:24:11 +03:00
1 parent 767428436d
commit 182bde8ac0
298 files changed
+35711 -5291

No files matched your search

+118 -42
View File
@@ -1,13 +1,18 @@
mod agent;
mod agent_catalog;
mod api_key;
mod approval;
mod auth;
mod connection;
mod import_job;
mod master_key;
mod observability;
mod onboarding;
mod operation;
mod operation_artifact;
mod operation_published;
mod pool_config;
mod product_event;
mod secret;
mod upstream;
mod workspace;
@@ -17,8 +22,8 @@ use crank_core::{
AgentId, AgentOperationBinding, AgentStatus, AgentVersion, ApprovalRequest, ApprovalRequestId,
AuthProfile, HttpMethod, InvitationId, InvitationToken, InvocationLog, InvocationLogId,
MembershipRole, OperationId, OperationStatus, PlatformApiKey, PlatformApiKeyId,
PlatformApiKeyStatus, Secret, SecretId, SecretVersion, Target, UsageRollup, User, UserId,
UserSessionId, Workspace, WorkspaceId,
PlatformApiKeyStatus, ProductEventId, Secret, SecretId, SecretVersion, Target, UsageRollup,
User, UserId, UserSessionId, Workspace, WorkspaceId,
};
use serde::{Serialize, de::DeserializeOwned};
use serde_json::Value;
@@ -30,25 +35,34 @@ pub use pool_config::{PostgresPoolConfig, PostgresPoolConfigError};
use crate::{
error::RegistryError,
model::{
AgentSummary, AgentVersionRecord, AppliedImportOperation, ApplyImportJobRequest,
ApprovalRequestRecord, AuthUserRecord, CreateAgentDraftVersionRequest, CreateAgentRequest,
CreateApprovalRequest, CreateImportJobRequest, CreateInvitationRequest,
CreateInvocationLogRequest, CreatePlatformApiKeyRequest, CreateSecretRequest,
CreateVersionRequest, CreateWorkspaceRequest, CreateYamlImportJobRequest,
DecideApprovalRequest, DescriptorMetadata, ExpireApprovalRequest, FinishApprovalRequest,
FinishImportJobRequest, ImportConflictMode, ImportJob, ImportJobApplyResult, ImportJobId,
ImportJobStatus, InvitationRecord, InvocationHistoryLoss, InvocationHistoryLossCategory,
InvocationHistoryWriteOutcome, InvocationLogRecord, ListApprovalRequestsQuery,
ListInvocationLogsQuery, MembershipRecord, OperationAgentRef, OperationSampleMetadata,
OperationSummary, OperationUsageSummary, OperationVersionRecord, PlatformApiKeyRecord,
AdminBootstrapContractRecord, AdminSecurityAuditRequest, AgentSummary, AgentVersionRecord,
AppendProductEventOutcome, AppendProductEventRequest, AppliedImportOperation,
ApplyImportJobRequest, ApprovalRequestRecord, AuthUserRecord,
ConsumeAdminBootstrapContractRequest, CreateAdminBootstrapContractRequest,
CreateAgentDraftVersionRequest, CreateAgentRequest, CreateApprovalRequest,
CreateImportJobRequest, CreateInvitationRequest, CreateInvocationLogRequest,
CreatePlatformApiKeyRequest, CreateSecretRequest, CreateVersionRequest,
CreateWorkspaceRequest, CreateYamlImportJobRequest, DecideApprovalRequest,
DescriptorMetadata, ExpireApprovalRequest, FinishApprovalRequest, FinishImportJobRequest,
ImportConflictMode, ImportJob, ImportJobApplyResult, ImportJobId, ImportJobStatus,
InvitationRecord, InvocationHistoryLoss, InvocationHistoryLossCategory,
InvocationHistoryWriteOutcome, InvocationLogRecord, InvocationRetentionOutcome,
InvocationRetentionPolicy, InvocationRetentionStatus, ListApprovalRequestsQuery,
ListInvocationLogsQuery, ListProductEventsQuery, MasterKeyIdentityCandidate,
MasterKeyIdentityRecord, MasterKeyRotationRecord, MasterKeyRotationStatus,
MembershipRecord, OnboardingMilestoneResult, OnboardingPresentationMilestone,
OperationAgentRef, OperationSampleMetadata, OperationStateExpectation, OperationSummary,
OperationUsageSummary, OperationVersionRecord, PlatformApiKeyRecord, ProductEventRecord,
PublishAgentRequest, PublishRequest, PublishedAgentCatalog, PublishedAgentTool,
RegistryOperation, RotateSecretRequest, SaveAgentBindingsRequest,
SaveAgentCatalogConfigRequest, SaveAuthProfileRequest, SaveDescriptorMetadataRequest,
SaveSampleMetadataRequest, SaveWorkspaceUpstreamRequest, SecretRecord, SecretVersionRecord,
SessionRecord, SkippedImportOperation, UpdateWorkspaceRequest, UsageAgentBreakdown,
UsageOperationBreakdown, UsageQuery, UsageRollupRecord, UsageSummary, UsageTimelinePoint,
WorkspaceMembershipRecord, WorkspaceRecord, WorkspaceUpstream, YamlImportJob,
YamlImportJobCompletion, YamlImportJobId, YamlImportJobStatus,
RecordOnboardingCompletionRequest, RecordOnboardingMilestoneRequest, RegistryOperation,
RotateSecretRequest, SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest,
SaveAuthProfileRequest, SaveDescriptorMetadataRequest, SaveSampleMetadataRequest,
SaveWorkspaceUpstreamRequest, SecretRecord, SecretVersionRecord, SessionRecord,
SkippedImportOperation, UpdateWorkspaceRequest, UsageAgentBreakdown,
UsageOperationBreakdown, UsageOutcomeBreakdown, UsageQuery, UsageRollupRecord,
UsageSummary, UsageTimelinePoint, WorkspaceMembershipRecord, WorkspaceRecord,
WorkspaceUpstream, YamlImportJob, YamlImportJobCompletion, YamlImportJobId,
YamlImportJobStatus,
},
};
@@ -106,12 +120,23 @@ async fn insert_version_row(
generated_draft_json,
config_export_json,
wizard_state_json,
name,
display_name,
category,
protocol,
security_level,
snapshot_provenance,
snapshot_observed_at,
published_at,
published_by,
change_note,
created_at,
created_by
) values (
$1, $2, $3, $4, $5, $6, $7, $8,
$9, $10, $11, $12, $13, $14, $15, $16::timestamptz, $17
$9, $10, $11, $12, $13, $14,
$15, $16, $17, $18, $19, 'native_v4', $20::timestamptz,
$21::timestamptz, $22, $23, $24::timestamptz, $25
)",
)
.bind(snapshot.id.as_str())
@@ -128,6 +153,17 @@ async fn insert_version_row(
.bind(serialize_option_json_value(&snapshot.generated_draft)?.map(Json))
.bind(serialize_option_json_value(&snapshot.config_export)?.map(Json))
.bind(serialize_option_json_value(&snapshot.wizard_state)?.map(Json))
.bind(&snapshot.name)
.bind(&snapshot.display_name)
.bind(&snapshot.category)
.bind(serialize_enum_text(&snapshot.protocol, "protocol")?)
.bind(serialize_enum_text(
&snapshot.security_level,
"security_level",
)?)
.bind(snapshot.updated_at)
.bind(snapshot.published_at)
.bind(Option::<&str>::None)
.bind(change_note)
.bind(snapshot.updated_at)
.bind(created_by)
@@ -222,10 +258,39 @@ async fn insert_agent_version_row(
async fn replace_agent_bindings_rows(
tx: &mut Transaction<'_, Postgres>,
workspace_id: &WorkspaceId,
agent_id: &AgentId,
version: u32,
bindings: &[AgentOperationBinding],
) -> Result<(), RegistryError> {
let operation_ids = bindings
.iter()
.map(|binding| binding.operation_id.as_str())
.collect::<std::collections::BTreeSet<_>>();
for operation_id in operation_ids {
let status = sqlx::query_scalar::<_, String>(
"select status from operations
where workspace_id = $1 and id = $2
for update",
)
.bind(workspace_id.as_str())
.bind(operation_id)
.fetch_optional(&mut **tx)
.await?;
match status.as_deref() {
Some("archived") => {
return Err(RegistryError::OperationArchived {
operation_id: operation_id.to_owned(),
});
}
Some(_) => {}
None => {
return Err(RegistryError::OperationNotFound {
operation_id: operation_id.to_owned(),
});
}
}
}
sqlx::query(
"delete from agent_operation_bindings
where agent_id = $1 and agent_version = $2",
@@ -263,27 +328,6 @@ async fn replace_agent_bindings_rows(
Ok(())
}
fn assert_immutable_fields(
summary: &OperationSummary,
snapshot: &RegistryOperation,
) -> Result<(), RegistryError> {
if summary.name != snapshot.name {
return Err(RegistryError::ImmutableOperationFieldChanged {
operation_id: snapshot.id.as_str().to_owned(),
field: "name",
});
}
if summary.protocol != snapshot.protocol {
return Err(RegistryError::ImmutableOperationFieldChanged {
operation_id: snapshot.id.as_str().to_owned(),
field: "protocol",
});
}
Ok(())
}
fn build_user(
id: String,
email: String,
@@ -324,7 +368,19 @@ fn map_invocation_log_record(row: &PgRow) -> Result<InvocationLogRecord, Registr
agent_id: row
.try_get::<Option<String>, _>("agent_id")?
.map(AgentId::new),
platform_api_key_id: row
.try_get::<Option<String>, _>("platform_api_key_id")?
.map(PlatformApiKeyId::new),
operation_id: OperationId::new(row.try_get::<String, _>("operation_id")?),
operation_version: row
.try_get::<Option<i32>, _>("operation_version")?
.map(|value| {
u32::try_from(value).map_err(|_| RegistryError::InvalidNumericValue {
field: "operation_version",
value: i64::from(value),
})
})
.transpose()?,
source: deserialize_enum_text(&row.try_get::<String, _>("source")?, "source")?,
level: deserialize_enum_text(&row.try_get::<String, _>("level")?, "level")?,
status: deserialize_enum_text(&row.try_get::<String, _>("status")?, "status")?,
@@ -345,6 +401,22 @@ fn map_invocation_log_record(row: &PgRow) -> Result<InvocationLogRecord, Registr
},
duration_ms: to_u64(row.try_get::<i64, _>("duration_ms")?, "duration_ms")?,
error_kind: row.try_get("error_kind")?,
execution_stage: row
.try_get::<Option<String>, _>("execution_stage")?
.map(|value| deserialize_enum_text(&value, "execution_stage"))
.transpose()?,
execution_error_code: row
.try_get::<Option<String>, _>("execution_error_code")?
.map(|value| deserialize_enum_text(&value, "execution_error_code"))
.transpose()?,
retryability: row
.try_get::<Option<String>, _>("retryability")?
.map(|value| deserialize_enum_text(&value, "retryability"))
.transpose()?,
outcome_certainty: row
.try_get::<Option<String>, _>("outcome_certainty")?
.map(|value| deserialize_enum_text(&value, "outcome_certainty"))
.transpose()?,
request_preview: row.try_get::<Json<Value>, _>("request_preview_json")?.0,
response_preview: row.try_get::<Json<Value>, _>("response_preview_json")?.0,
created_at: row.try_get("created_at")?,
@@ -423,6 +495,7 @@ fn build_agent_summary(
status: String,
current_draft_version: i32,
latest_published_version: Option<i32>,
catalog_revision: i64,
created_at: OffsetDateTime,
updated_at: OffsetDateTime,
published_at: Option<OffsetDateTime>,
@@ -438,6 +511,7 @@ fn build_agent_summary(
latest_published_version: latest_published_version
.map(|value| from_db_version(value, "latest_published_version"))
.transpose()?,
catalog_revision,
created_at,
updated_at,
published_at,
@@ -457,6 +531,7 @@ fn build_operation_summary(
status: String,
current_draft_version: i32,
latest_published_version: Option<i32>,
can_delete: bool,
created_at: OffsetDateTime,
updated_at: OffsetDateTime,
published_at: Option<OffsetDateTime>,
@@ -479,6 +554,7 @@ fn build_operation_summary(
latest_published_version: latest_published_version
.map(|value| from_db_version(value, "latest_published_version"))
.transpose()?,
can_delete,
created_at,
updated_at,
published_at,