fix(epic-1): repair atomic agent edits and dispatch certainty
This commit is contained in:
1 parent
82b215dd74
commit
7aa854de26
16 files changed
+1600
-57
No files matched your search
@@ -265,16 +265,111 @@ impl PostgresRegistry {
|
||||
&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
|
||||
}
|
||||
|
||||
async fn save_agent_catalog_config_inner(
|
||||
&self,
|
||||
request: SaveAgentCatalogConfigRequest<'_>,
|
||||
allow_fork: bool,
|
||||
) -> Result<u32, RegistryError> {
|
||||
let mut tx = self.pool.begin().await?;
|
||||
Self::lock_agent_and_validate_expected_state(
|
||||
let agent_row = 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?;
|
||||
|
||||
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?;
|
||||
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
|
||||
@@ -309,7 +404,7 @@ impl PostgresRegistry {
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
Ok(())
|
||||
Ok(request.agent_version)
|
||||
}
|
||||
|
||||
async fn lock_agent_and_validate_expected_state(
|
||||
@@ -317,7 +412,7 @@ impl PostgresRegistry {
|
||||
workspace_id: &WorkspaceId,
|
||||
agent_id: &AgentId,
|
||||
expected_state: Option<&AgentStateExpectation>,
|
||||
) -> Result<(), RegistryError> {
|
||||
) -> Result<sqlx::postgres::PgRow, RegistryError> {
|
||||
let row = sqlx::query(
|
||||
"select
|
||||
status,
|
||||
@@ -364,7 +459,7 @@ impl PostgresRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
async fn reject_published_agent_version(
|
||||
|
||||
Reference in new issue
Block a user