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.
953 lines
33 KiB
Rust
953 lines
33 KiB
Rust
use super::agent_prompt_bindings::{
|
||
ensure_agent_prompt_bindings_not_archived, replace_agent_prompt_bindings_rows,
|
||
};
|
||
use super::agent_resource_bindings::{
|
||
ensure_agent_bindings_not_revoked, replace_agent_resource_bindings_rows,
|
||
};
|
||
use super::*;
|
||
use crate::model::{AgentStateExpectation, UpdateAgentSummaryRequest};
|
||
|
||
impl PostgresRegistry {
|
||
pub async fn list_agents(
|
||
&self,
|
||
workspace_id: &WorkspaceId,
|
||
) -> Result<Vec<AgentSummary>, RegistryError> {
|
||
let rows = sqlx::query(
|
||
"select
|
||
id,
|
||
workspace_id,
|
||
slug,
|
||
display_name,
|
||
description,
|
||
status,
|
||
current_draft_version,
|
||
latest_published_version,
|
||
catalog_revision,
|
||
created_at,
|
||
updated_at,
|
||
published_at
|
||
from agents
|
||
where workspace_id = $1
|
||
order by slug asc",
|
||
)
|
||
.bind(workspace_id.as_str())
|
||
.fetch_all(&self.pool)
|
||
.await?;
|
||
|
||
rows.into_iter()
|
||
.map(|row| {
|
||
build_agent_summary(
|
||
row.try_get("id")?,
|
||
row.try_get("workspace_id")?,
|
||
row.try_get("slug")?,
|
||
row.try_get("display_name")?,
|
||
row.try_get("description")?,
|
||
row.try_get("status")?,
|
||
row.try_get("current_draft_version")?,
|
||
row.try_get("latest_published_version")?,
|
||
row.try_get("catalog_revision")?,
|
||
row.try_get("created_at")?,
|
||
row.try_get("updated_at")?,
|
||
row.try_get("published_at")?,
|
||
)
|
||
})
|
||
.collect()
|
||
}
|
||
|
||
pub async fn get_agent_summary(
|
||
&self,
|
||
workspace_id: &WorkspaceId,
|
||
agent_id: &AgentId,
|
||
) -> Result<Option<AgentSummary>, RegistryError> {
|
||
let row = sqlx::query(
|
||
"select
|
||
id,
|
||
workspace_id,
|
||
slug,
|
||
display_name,
|
||
description,
|
||
status,
|
||
current_draft_version,
|
||
latest_published_version,
|
||
catalog_revision,
|
||
created_at,
|
||
updated_at,
|
||
published_at
|
||
from agents
|
||
where workspace_id = $1 and id = $2",
|
||
)
|
||
.bind(workspace_id.as_str())
|
||
.bind(agent_id.as_str())
|
||
.fetch_optional(&self.pool)
|
||
.await?;
|
||
|
||
row.map(|row| {
|
||
build_agent_summary(
|
||
row.try_get("id")?,
|
||
row.try_get("workspace_id")?,
|
||
row.try_get("slug")?,
|
||
row.try_get("display_name")?,
|
||
row.try_get("description")?,
|
||
row.try_get("status")?,
|
||
row.try_get("current_draft_version")?,
|
||
row.try_get("latest_published_version")?,
|
||
row.try_get("catalog_revision")?,
|
||
row.try_get("created_at")?,
|
||
row.try_get("updated_at")?,
|
||
row.try_get("published_at")?,
|
||
)
|
||
})
|
||
.transpose()
|
||
}
|
||
|
||
pub async fn create_agent(&self, request: CreateAgentRequest<'_>) -> Result<(), RegistryError> {
|
||
let mut tx = self.pool.begin().await?;
|
||
|
||
sqlx::query(
|
||
"insert into agents (
|
||
id,
|
||
workspace_id,
|
||
slug,
|
||
display_name,
|
||
description,
|
||
status,
|
||
current_draft_version,
|
||
latest_published_version,
|
||
created_at,
|
||
updated_at,
|
||
published_at
|
||
) values (
|
||
$1, $2, $3, $4, $5, $6, $7, $8, $9::timestamptz, $10::timestamptz, $11::timestamptz
|
||
)",
|
||
)
|
||
.bind(request.agent.id.as_str())
|
||
.bind(request.agent.workspace_id.as_str())
|
||
.bind(&request.agent.slug)
|
||
.bind(&request.agent.display_name)
|
||
.bind(&request.agent.description)
|
||
.bind(serialize_enum_text(&request.agent.status, "status")?)
|
||
.bind(to_db_version(request.agent.current_draft_version))
|
||
.bind(request.agent.latest_published_version.map(to_db_version))
|
||
.bind(request.agent.created_at)
|
||
.bind(request.agent.updated_at)
|
||
.bind(request.agent.published_at)
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
|
||
insert_agent_version_row(&mut tx, request.version).await?;
|
||
replace_agent_bindings_rows(
|
||
&mut tx,
|
||
&request.agent.workspace_id,
|
||
&request.agent.id,
|
||
request.version.version,
|
||
request.bindings,
|
||
)
|
||
.await?;
|
||
|
||
tx.commit().await?;
|
||
Ok(())
|
||
}
|
||
|
||
pub async fn create_agent_draft_version(
|
||
&self,
|
||
request: CreateAgentDraftVersionRequest<'_>,
|
||
) -> Result<(), RegistryError> {
|
||
let Some(summary) = self
|
||
.get_agent_summary(request.workspace_id, request.agent_id)
|
||
.await?
|
||
else {
|
||
return Err(RegistryError::AgentNotFound {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
});
|
||
};
|
||
|
||
let expected = summary.current_draft_version + 1;
|
||
if request.version.version != expected {
|
||
return Err(RegistryError::InvalidAgentVersionSequence {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
expected,
|
||
actual: request.version.version,
|
||
});
|
||
}
|
||
|
||
let mut tx = self.pool.begin().await?;
|
||
insert_agent_version_row(&mut tx, request.version).await?;
|
||
replace_agent_bindings_rows(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
request.version.version,
|
||
request.bindings,
|
||
)
|
||
.await?;
|
||
|
||
sqlx::query(
|
||
"update agents
|
||
set current_draft_version = $3,
|
||
updated_at = $4::timestamptz
|
||
where workspace_id = $1 and id = $2",
|
||
)
|
||
.bind(request.workspace_id.as_str())
|
||
.bind(request.agent_id.as_str())
|
||
.bind(to_db_version(request.version.version))
|
||
.bind(request.updated_at)
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
|
||
tx.commit().await?;
|
||
Ok(())
|
||
}
|
||
|
||
pub async fn get_agent_version(
|
||
&self,
|
||
workspace_id: &WorkspaceId,
|
||
agent_id: &AgentId,
|
||
version: u32,
|
||
) -> Result<Option<AgentVersionRecord>, RegistryError> {
|
||
let summary = match self.get_agent_summary(workspace_id, agent_id).await? {
|
||
Some(value) => value,
|
||
None => return Ok(None),
|
||
};
|
||
|
||
let row = sqlx::query!(
|
||
"select
|
||
agent_id,
|
||
version,
|
||
status,
|
||
instructions_json,
|
||
tool_selection_policy_json,
|
||
created_at as \"created_at!: time::OffsetDateTime\"
|
||
from agent_versions
|
||
where agent_id = $1 and version = $2",
|
||
agent_id.as_str(),
|
||
to_db_version(version),
|
||
)
|
||
.fetch_optional(&self.pool)
|
||
.await?;
|
||
|
||
let Some(row) = row else {
|
||
return Ok(None);
|
||
};
|
||
|
||
let bindings = self.list_agent_bindings(agent_id, version).await?;
|
||
let resource_bindings = self.list_agent_resource_bindings(agent_id, version).await?;
|
||
let prompt_bindings = self.list_agent_prompt_bindings(agent_id, version).await?;
|
||
Ok(Some(build_agent_version_record(
|
||
&summary,
|
||
row.version,
|
||
row.status,
|
||
row.instructions_json,
|
||
row.tool_selection_policy_json,
|
||
row.created_at,
|
||
bindings,
|
||
resource_bindings,
|
||
prompt_bindings,
|
||
)?))
|
||
}
|
||
|
||
pub async fn save_agent_bindings(
|
||
&self,
|
||
request: SaveAgentBindingsRequest<'_>,
|
||
) -> Result<(), RegistryError> {
|
||
let mut tx = self.pool.begin().await?;
|
||
Self::lock_agent_and_validate_expected_state(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
request.expected_state,
|
||
)
|
||
.await?;
|
||
Self::reject_published_agent_version(&mut tx, request.agent_id, request.agent_version)
|
||
.await?;
|
||
replace_agent_bindings_rows(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
request.agent_version,
|
||
request.bindings,
|
||
)
|
||
.await?;
|
||
tx.commit().await?;
|
||
Ok(())
|
||
}
|
||
|
||
pub async fn save_agent_catalog_config(
|
||
&self,
|
||
request: SaveAgentCatalogConfigRequest<'_>,
|
||
) -> Result<(), RegistryError> {
|
||
self.save_agent_catalog_config_inner(request, false, &[])
|
||
.await
|
||
.map(|_| ())
|
||
}
|
||
|
||
pub async fn save_agent_catalog_config_editable(
|
||
&self,
|
||
request: SaveAgentCatalogConfigRequest<'_>,
|
||
) -> Result<u32, RegistryError> {
|
||
self.save_agent_catalog_config_inner(request, true, &[])
|
||
.await
|
||
}
|
||
|
||
/// Как [`Self::save_agent_catalog_config_editable`], но с точными Prompt
|
||
/// bindings Draft-версии (Story 3.13). Существующие вызовы без Prompt
|
||
/// сохраняют прежнюю сигнатуру и семантику.
|
||
pub async fn save_agent_catalog_config_editable_with_prompts(
|
||
&self,
|
||
request: SaveAgentCatalogConfigRequest<'_>,
|
||
prompt_bindings: &[AgentPromptBinding],
|
||
) -> Result<u32, RegistryError> {
|
||
self.save_agent_catalog_config_inner(request, true, prompt_bindings)
|
||
.await
|
||
}
|
||
|
||
async fn save_agent_catalog_config_inner(
|
||
&self,
|
||
request: SaveAgentCatalogConfigRequest<'_>,
|
||
allow_fork: bool,
|
||
prompt_bindings: &[AgentPromptBinding],
|
||
) -> Result<u32, RegistryError> {
|
||
let mut tx = self.pool.begin().await?;
|
||
let agent_row = Self::lock_agent_and_validate_expected_state(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
request.expected_state,
|
||
)
|
||
.await?;
|
||
|
||
if allow_fork {
|
||
let status =
|
||
deserialize_enum_text::<AgentStatus>(agent_row.try_get("status")?, "status")?;
|
||
if status == AgentStatus::Archived {
|
||
return Err(RegistryError::InvalidAgentTransition {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
from: "archived".to_owned(),
|
||
action: "edit",
|
||
});
|
||
}
|
||
let current_draft_version = from_db_version(
|
||
agent_row.try_get("current_draft_version")?,
|
||
"current_draft_version",
|
||
)?;
|
||
if request.agent_version != current_draft_version {
|
||
return Err(RegistryError::InvalidAgentVersionSequence {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
expected: current_draft_version,
|
||
actual: request.agent_version,
|
||
});
|
||
}
|
||
let latest_published_version = agent_row
|
||
.try_get::<Option<i32>, _>("latest_published_version")?
|
||
.map(|value| from_db_version(value, "latest_published_version"))
|
||
.transpose()?;
|
||
|
||
if latest_published_version == Some(current_draft_version) {
|
||
let instructions = sqlx::query(
|
||
"select instructions_json
|
||
from agent_versions
|
||
where agent_id = $1 and version = $2",
|
||
)
|
||
.bind(request.agent_id.as_str())
|
||
.bind(to_db_version(current_draft_version))
|
||
.fetch_optional(&mut *tx)
|
||
.await?
|
||
.ok_or_else(|| RegistryError::AgentNotFound {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
})?
|
||
.try_get::<Value, _>("instructions_json")?;
|
||
|
||
let now = time::OffsetDateTime::now_utc();
|
||
let draft = AgentVersion {
|
||
agent_id: request.agent_id.clone(),
|
||
version: current_draft_version + 1,
|
||
status: AgentStatus::Draft,
|
||
instructions,
|
||
tool_selection_policy: request.tool_selection_policy.clone(),
|
||
created_at: now,
|
||
};
|
||
insert_agent_version_row(&mut tx, &draft).await?;
|
||
replace_agent_bindings_rows(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
draft.version,
|
||
request.bindings,
|
||
)
|
||
.await?;
|
||
replace_agent_resource_bindings_rows(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
draft.version,
|
||
request.resource_bindings,
|
||
)
|
||
.await?;
|
||
replace_agent_prompt_bindings_rows(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
draft.version,
|
||
prompt_bindings,
|
||
)
|
||
.await?;
|
||
sqlx::query(
|
||
"update agents
|
||
set current_draft_version = $3,
|
||
updated_at = $4::timestamptz
|
||
where workspace_id = $1 and id = $2",
|
||
)
|
||
.bind(request.workspace_id.as_str())
|
||
.bind(request.agent_id.as_str())
|
||
.bind(to_db_version(draft.version))
|
||
.bind(now)
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
tx.commit().await?;
|
||
return Ok(draft.version);
|
||
}
|
||
} else {
|
||
Self::reject_published_agent_version(&mut tx, request.agent_id, request.agent_version)
|
||
.await?;
|
||
}
|
||
|
||
let updated = sqlx::query(
|
||
"update agent_versions
|
||
set tool_selection_policy_json = $3
|
||
where agent_id = $1 and version = $2",
|
||
)
|
||
.bind(request.agent_id.as_str())
|
||
.bind(to_db_version(request.agent_version))
|
||
.bind(Json(serialize_json_value(request.tool_selection_policy)?))
|
||
.execute(&mut *tx)
|
||
.await?
|
||
.rows_affected();
|
||
if updated == 0 {
|
||
return Err(RegistryError::AgentNotFound {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
});
|
||
}
|
||
replace_agent_bindings_rows(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
request.agent_version,
|
||
request.bindings,
|
||
)
|
||
.await?;
|
||
replace_agent_resource_bindings_rows(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
request.agent_version,
|
||
request.resource_bindings,
|
||
)
|
||
.await?;
|
||
replace_agent_prompt_bindings_rows(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
request.agent_version,
|
||
prompt_bindings,
|
||
)
|
||
.await?;
|
||
sqlx::query(
|
||
"update agents
|
||
set updated_at = now()
|
||
where id = $1 and workspace_id = $2",
|
||
)
|
||
.bind(request.agent_id.as_str())
|
||
.bind(request.workspace_id.as_str())
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
tx.commit().await?;
|
||
Ok(request.agent_version)
|
||
}
|
||
|
||
async fn lock_agent_and_validate_expected_state(
|
||
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||
workspace_id: &WorkspaceId,
|
||
agent_id: &AgentId,
|
||
expected_state: Option<&AgentStateExpectation>,
|
||
) -> Result<sqlx::postgres::PgRow, RegistryError> {
|
||
let row = sqlx::query(
|
||
"select
|
||
status,
|
||
current_draft_version,
|
||
latest_published_version,
|
||
catalog_revision,
|
||
updated_at
|
||
from agents
|
||
where workspace_id = $1 and id = $2
|
||
for update",
|
||
)
|
||
.bind(workspace_id.as_str())
|
||
.bind(agent_id.as_str())
|
||
.fetch_optional(&mut **tx)
|
||
.await?;
|
||
let Some(row) = row else {
|
||
return Err(RegistryError::AgentNotFound {
|
||
agent_id: agent_id.as_str().to_owned(),
|
||
});
|
||
};
|
||
|
||
if let Some(expected) = expected_state {
|
||
let status = deserialize_enum_text::<AgentStatus>(row.try_get("status")?, "status")?;
|
||
let current_draft_version = from_db_version(
|
||
row.try_get("current_draft_version")?,
|
||
"current_draft_version",
|
||
)?;
|
||
let latest_published_version = row
|
||
.try_get::<Option<i32>, _>("latest_published_version")?
|
||
.map(|value| from_db_version(value, "latest_published_version"))
|
||
.transpose()?;
|
||
let catalog_revision: i64 = row.try_get("catalog_revision")?;
|
||
let updated_at: time::OffsetDateTime = row.try_get("updated_at")?;
|
||
|
||
if status != expected.status
|
||
|| current_draft_version != expected.current_draft_version
|
||
|| latest_published_version != expected.latest_published_version
|
||
|| catalog_revision != expected.catalog_revision
|
||
|| updated_at != expected.updated_at
|
||
{
|
||
return Err(RegistryError::AgentStaleRevision {
|
||
agent_id: agent_id.as_str().to_owned(),
|
||
});
|
||
}
|
||
}
|
||
|
||
Ok(row)
|
||
}
|
||
|
||
async fn reject_published_agent_version(
|
||
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||
agent_id: &AgentId,
|
||
version: u32,
|
||
) -> Result<(), RegistryError> {
|
||
let row = sqlx::query(
|
||
"select status
|
||
from agent_versions
|
||
where agent_id = $1 and version = $2
|
||
for update",
|
||
)
|
||
.bind(agent_id.as_str())
|
||
.bind(to_db_version(version))
|
||
.fetch_optional(&mut **tx)
|
||
.await?;
|
||
let Some(row) = row else {
|
||
return Err(RegistryError::AgentNotFound {
|
||
agent_id: agent_id.as_str().to_owned(),
|
||
});
|
||
};
|
||
let status = deserialize_enum_text::<AgentStatus>(row.try_get("status")?, "status")?;
|
||
if status == AgentStatus::Published {
|
||
return Err(RegistryError::ImmutableAgentVersion {
|
||
agent_id: agent_id.as_str().to_owned(),
|
||
version,
|
||
});
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
pub async fn update_agent_summary(
|
||
&self,
|
||
workspace_id: &WorkspaceId,
|
||
agent_id: &AgentId,
|
||
update: UpdateAgentSummaryRequest<'_>,
|
||
) -> Result<(), RegistryError> {
|
||
let mut tx = self.pool.begin().await?;
|
||
Self::lock_agent_and_validate_expected_state(
|
||
&mut tx,
|
||
workspace_id,
|
||
agent_id,
|
||
update.expected_state,
|
||
)
|
||
.await?;
|
||
let result = sqlx::query(
|
||
"update agents
|
||
set slug = $3,
|
||
display_name = $4,
|
||
description = $5,
|
||
updated_at = $6::timestamptz
|
||
where workspace_id = $1 and id = $2",
|
||
)
|
||
.bind(workspace_id.as_str())
|
||
.bind(agent_id.as_str())
|
||
.bind(update.slug)
|
||
.bind(update.display_name)
|
||
.bind(update.description)
|
||
.bind(update.updated_at)
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
|
||
if result.rows_affected() == 0 {
|
||
return Err(RegistryError::AgentNotFound {
|
||
agent_id: agent_id.as_str().to_owned(),
|
||
});
|
||
}
|
||
|
||
tx.commit().await?;
|
||
Ok(())
|
||
}
|
||
|
||
pub async fn delete_agent(
|
||
&self,
|
||
workspace_id: &WorkspaceId,
|
||
agent_id: &AgentId,
|
||
expected_state: Option<&AgentStateExpectation>,
|
||
) -> Result<(), RegistryError> {
|
||
let mut tx = self.pool.begin().await?;
|
||
Self::lock_agent_and_validate_expected_state(
|
||
&mut tx,
|
||
workspace_id,
|
||
agent_id,
|
||
expected_state,
|
||
)
|
||
.await?;
|
||
let result = sqlx::query(
|
||
"delete from agents
|
||
where workspace_id = $1
|
||
and id = $2
|
||
and latest_published_version is null",
|
||
)
|
||
.bind(workspace_id.as_str())
|
||
.bind(agent_id.as_str())
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
|
||
if result.rows_affected() == 0 {
|
||
return Err(RegistryError::InvalidAgentTransition {
|
||
agent_id: agent_id.as_str().to_owned(),
|
||
from: "published".to_owned(),
|
||
action: "delete",
|
||
});
|
||
}
|
||
|
||
tx.commit().await?;
|
||
Ok(())
|
||
}
|
||
|
||
pub async fn publish_agent(
|
||
&self,
|
||
request: PublishAgentRequest<'_>,
|
||
) -> Result<(), RegistryError> {
|
||
let mut tx = self.pool.begin().await?;
|
||
let Some(agent_row) = sqlx::query(
|
||
"select status, current_draft_version, latest_published_version, catalog_revision, updated_at
|
||
from agents
|
||
where id = $1 and workspace_id = $2
|
||
for update",
|
||
)
|
||
.bind(request.agent_id.as_str())
|
||
.bind(request.workspace_id.as_str())
|
||
.fetch_optional(&mut *tx)
|
||
.await?
|
||
else {
|
||
return Err(RegistryError::AgentNotFound {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
});
|
||
};
|
||
if let Some(expected) = request.expected_state {
|
||
let status =
|
||
deserialize_enum_text::<AgentStatus>(agent_row.try_get("status")?, "status")?;
|
||
let expected_current_draft_version = from_db_version(
|
||
agent_row.try_get("current_draft_version")?,
|
||
"current_draft_version",
|
||
)?;
|
||
let expected_latest_published_version = agent_row
|
||
.try_get::<Option<i32>, _>("latest_published_version")?
|
||
.map(|value| from_db_version(value, "latest_published_version"))
|
||
.transpose()?;
|
||
let expected_catalog_revision: i64 = agent_row.try_get("catalog_revision")?;
|
||
let expected_updated_at: time::OffsetDateTime = agent_row.try_get("updated_at")?;
|
||
if status != expected.status
|
||
|| expected_current_draft_version != expected.current_draft_version
|
||
|| expected_latest_published_version != expected.latest_published_version
|
||
|| expected_catalog_revision != expected.catalog_revision
|
||
|| expected_updated_at != expected.updated_at
|
||
{
|
||
return Err(RegistryError::AgentStaleRevision {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
});
|
||
}
|
||
}
|
||
let agent_status =
|
||
deserialize_enum_text::<AgentStatus>(agent_row.try_get("status")?, "status")?;
|
||
if agent_status == AgentStatus::Archived {
|
||
return Err(RegistryError::InvalidAgentTransition {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
from: "archived".to_owned(),
|
||
action: "publish",
|
||
});
|
||
}
|
||
let current_draft_version = from_db_version(
|
||
agent_row.try_get("current_draft_version")?,
|
||
"current_draft_version",
|
||
)?;
|
||
if request.version != current_draft_version {
|
||
return Err(RegistryError::InvalidAgentVersionSequence {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
expected: current_draft_version,
|
||
actual: request.version,
|
||
});
|
||
}
|
||
let latest_published_version = agent_row
|
||
.try_get::<Option<i32>, _>("latest_published_version")?
|
||
.map(|value| from_db_version(value, "latest_published_version"))
|
||
.transpose()?;
|
||
if latest_published_version.is_some_and(|latest| request.version < latest) {
|
||
return Err(RegistryError::InvalidAgentVersionSequence {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
expected: latest_published_version.unwrap_or(request.version),
|
||
actual: request.version,
|
||
});
|
||
}
|
||
// Повторная публикация уже опубликованной версии идемпотентна и не даёт
|
||
// второго bump catalog_revision (Story 3.13 AC2). После unpublish Agent
|
||
// снова Draft, поэтому такое состояние требует обычной публикации.
|
||
if latest_published_version == Some(request.version)
|
||
&& agent_status == AgentStatus::Published
|
||
{
|
||
return Ok(());
|
||
}
|
||
let catalog_revision: i64 = agent_row.try_get("catalog_revision")?;
|
||
let next_catalog_revision = catalog_revision
|
||
.checked_add(1)
|
||
.filter(|value| *value > 0)
|
||
.ok_or(RegistryError::InvalidNumericValue {
|
||
field: "catalog_revision",
|
||
value: catalog_revision,
|
||
})?;
|
||
|
||
let version_exists = sqlx::query(
|
||
"select 1
|
||
from agent_versions
|
||
where agent_id = $1 and version = $2",
|
||
)
|
||
.bind(request.agent_id.as_str())
|
||
.bind(to_db_version(request.version))
|
||
.fetch_optional(&mut *tx)
|
||
.await?
|
||
.is_some();
|
||
if !version_exists {
|
||
return Err(RegistryError::AgentNotFound {
|
||
agent_id: request.agent_id.as_str().to_owned(),
|
||
});
|
||
}
|
||
|
||
// Story 3.8: публикация Agent — тоже точка выдачи Resource-доступа. Текущая
|
||
// версия bindings перечитывается внутри этой транзакции после Agent lock,
|
||
// Resources блокируются в порядке resource_id ASC, и отозванный Resource
|
||
// отменяет публикацию до любой мутации каталога.
|
||
ensure_agent_bindings_not_revoked(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
request.version,
|
||
)
|
||
.await?;
|
||
// Story 3.13: archived Prompt не может войти в новую публикацию Agent.
|
||
ensure_agent_prompt_bindings_not_archived(
|
||
&mut tx,
|
||
request.workspace_id,
|
||
request.agent_id,
|
||
request.version,
|
||
)
|
||
.await?;
|
||
|
||
sqlx::query(
|
||
"update agent_versions
|
||
set status = $1
|
||
where agent_id = $2 and version = $3",
|
||
)
|
||
.bind(serialize_enum_text(&AgentStatus::Published, "status")?)
|
||
.bind(request.agent_id.as_str())
|
||
.bind(to_db_version(request.version))
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
|
||
sqlx::query(
|
||
"insert into published_agents (
|
||
agent_id,
|
||
version,
|
||
catalog_revision,
|
||
published_at,
|
||
published_by
|
||
) values ($1, $2, $3, $4::timestamptz, $5)
|
||
on conflict(agent_id) do update set
|
||
version = excluded.version,
|
||
catalog_revision = excluded.catalog_revision,
|
||
published_at = excluded.published_at,
|
||
published_by = excluded.published_by",
|
||
)
|
||
.bind(request.agent_id.as_str())
|
||
.bind(to_db_version(request.version))
|
||
.bind(next_catalog_revision)
|
||
.bind(request.published_at)
|
||
.bind(request.published_by)
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
|
||
sqlx::query(
|
||
"update agents
|
||
set status = $1,
|
||
latest_published_version = $2,
|
||
catalog_revision = $3,
|
||
published_at = $4::timestamptz,
|
||
updated_at = $5::timestamptz
|
||
where id = $6 and workspace_id = $7",
|
||
)
|
||
.bind(serialize_enum_text(&AgentStatus::Published, "status")?)
|
||
.bind(to_db_version(request.version))
|
||
.bind(next_catalog_revision)
|
||
.bind(request.published_at)
|
||
.bind(request.published_at)
|
||
.bind(request.agent_id.as_str())
|
||
.bind(request.workspace_id.as_str())
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
|
||
tx.commit().await?;
|
||
Ok(())
|
||
}
|
||
|
||
pub async fn unpublish_agent(
|
||
&self,
|
||
workspace_id: &WorkspaceId,
|
||
agent_id: &AgentId,
|
||
updated_at: &time::OffsetDateTime,
|
||
expected_state: Option<&AgentStateExpectation>,
|
||
) -> Result<(), RegistryError> {
|
||
let mut tx = self.pool.begin().await?;
|
||
Self::lock_agent_and_validate_expected_state(
|
||
&mut tx,
|
||
workspace_id,
|
||
agent_id,
|
||
expected_state,
|
||
)
|
||
.await?;
|
||
|
||
let current_status = sqlx::query(
|
||
"select status, catalog_revision
|
||
from agents
|
||
where id = $1 and workspace_id = $2
|
||
for update",
|
||
)
|
||
.bind(agent_id.as_str())
|
||
.bind(workspace_id.as_str())
|
||
.fetch_one(&mut *tx)
|
||
.await?;
|
||
let status =
|
||
deserialize_enum_text::<AgentStatus>(current_status.try_get("status")?, "status")?;
|
||
if status != AgentStatus::Published {
|
||
return Err(RegistryError::InvalidAgentTransition {
|
||
agent_id: agent_id.as_str().to_owned(),
|
||
from: serialize_enum_text(&status, "status")?,
|
||
action: "unpublish",
|
||
});
|
||
}
|
||
let catalog_revision: i64 = current_status.try_get("catalog_revision")?;
|
||
let next_catalog_revision = catalog_revision
|
||
.checked_add(1)
|
||
.filter(|value| *value > 0)
|
||
.ok_or(RegistryError::InvalidNumericValue {
|
||
field: "catalog_revision",
|
||
value: catalog_revision,
|
||
})?;
|
||
|
||
let result = sqlx::query(
|
||
"update agents
|
||
set status = $1,
|
||
catalog_revision = $2,
|
||
published_at = null,
|
||
updated_at = $3::timestamptz
|
||
where id = $4 and workspace_id = $5",
|
||
)
|
||
.bind(serialize_enum_text(&AgentStatus::Draft, "status")?)
|
||
.bind(next_catalog_revision)
|
||
.bind(updated_at)
|
||
.bind(agent_id.as_str())
|
||
.bind(workspace_id.as_str())
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
|
||
if result.rows_affected() == 0 {
|
||
return Err(RegistryError::AgentNotFound {
|
||
agent_id: agent_id.as_str().to_owned(),
|
||
});
|
||
}
|
||
|
||
tx.commit().await?;
|
||
Ok(())
|
||
}
|
||
|
||
pub async fn archive_agent(
|
||
&self,
|
||
workspace_id: &WorkspaceId,
|
||
agent_id: &AgentId,
|
||
updated_at: &time::OffsetDateTime,
|
||
expected_state: Option<&AgentStateExpectation>,
|
||
) -> Result<(), RegistryError> {
|
||
let mut tx = self.pool.begin().await?;
|
||
Self::lock_agent_and_validate_expected_state(
|
||
&mut tx,
|
||
workspace_id,
|
||
agent_id,
|
||
expected_state,
|
||
)
|
||
.await?;
|
||
|
||
let current_status = sqlx::query(
|
||
"select status, catalog_revision
|
||
from agents
|
||
where id = $1 and workspace_id = $2
|
||
for update",
|
||
)
|
||
.bind(agent_id.as_str())
|
||
.bind(workspace_id.as_str())
|
||
.fetch_one(&mut *tx)
|
||
.await?;
|
||
let status =
|
||
deserialize_enum_text::<AgentStatus>(current_status.try_get("status")?, "status")?;
|
||
if status == AgentStatus::Archived {
|
||
return Err(RegistryError::InvalidAgentTransition {
|
||
agent_id: agent_id.as_str().to_owned(),
|
||
from: "archived".to_owned(),
|
||
action: "archive",
|
||
});
|
||
}
|
||
let catalog_revision: i64 = current_status.try_get("catalog_revision")?;
|
||
let next_catalog_revision = catalog_revision
|
||
.checked_add(1)
|
||
.filter(|value| *value > 0)
|
||
.ok_or(RegistryError::InvalidNumericValue {
|
||
field: "catalog_revision",
|
||
value: catalog_revision,
|
||
})?;
|
||
|
||
let result = sqlx::query(
|
||
"update agents
|
||
set status = $1,
|
||
catalog_revision = $2,
|
||
published_at = null,
|
||
updated_at = $3::timestamptz
|
||
where id = $4 and workspace_id = $5",
|
||
)
|
||
.bind(serialize_enum_text(&AgentStatus::Archived, "status")?)
|
||
.bind(next_catalog_revision)
|
||
.bind(updated_at)
|
||
.bind(agent_id.as_str())
|
||
.bind(workspace_id.as_str())
|
||
.execute(&mut *tx)
|
||
.await?;
|
||
|
||
if result.rows_affected() == 0 {
|
||
return Err(RegistryError::AgentNotFound {
|
||
agent_id: agent_id.as_str().to_owned(),
|
||
});
|
||
}
|
||
|
||
tx.commit().await?;
|
||
Ok(())
|
||
}
|
||
}
|