api: gate create_loop + create_topic on empty workspace roster (W2)
Frontend already disables the create button when the roster is empty, but nothing stopped a direct POST from materializing an orphan loop / topic with nothing to staff it. Both handlers now check the workspace agent count up front and return 409 Conflict when it's zero. - crates/cm-api/src/routes/loops.rs: gate at top of create_loop - crates/cm-api/src/routes/research.rs: gate at top of create_topic Uses the existing cm_db::repo::agents::count_active(pool, ws) helper (count of workspace agents where deleted_at IS NULL). 409 is the right mapping: state-of-the-workspace-prevents-this, not client-input-bad.
This commit is contained in:
@@ -114,6 +114,13 @@ pub async fn create_loop(
|
||||
if body.title.trim().is_empty() || body.task_template.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest);
|
||||
}
|
||||
// Empty-roster gate: a workspace with zero agents has nothing to staff
|
||||
// the loop with — refuse before any DB writes. Frontend already
|
||||
// disables the create button in this state; this closes the direct-POST
|
||||
// hole so we don't materialize orphan loops that never fire.
|
||||
if cm_db::repo::agents::count_active(&state.pool, user.workspace_id).await? == 0 {
|
||||
return Err(ApiError::Conflict);
|
||||
}
|
||||
let webhook_enabled = parse_triggers(&body.triggers)
|
||||
.map(|t| t.webhook_enabled)
|
||||
.unwrap_or(false);
|
||||
|
||||
Reference in New Issue
Block a user