Context
Surfaced as a non-blocking suggestion in PR #1248 (code review item 4).
`Scheduler::restart_workflows_for_agent` calls `self.storage.list_workflows(None)` which loads all workflows from the database and filters by `agent_id` in Rust:
```rust
// scheduler/mod.rs
let workflows = self.storage.list_workflows(None).await?;
for workflow in workflows {
if workflow.agent_id != agent_id || !workflow.enabled {
continue;
}
...
}
```
`SchedulerStorage::list_workflows` currently only supports filtering by `project_id`, not `agent_id`. This means every agent restart performs a full table scan and discards unrelated rows in the application layer.
Why it matters
Restarts are rare so this is not an immediate problem, but as the workflow count grows this becomes a linear scan per restart. A targeted SQL query with a `WHERE agent_id = ?` clause would keep it constant regardless of total workflow count.
Proposed change
- Add a `list_workflows_for_agent` method to `SchedulerStorage`:
```rust
/// Lists all enabled workflows for a specific agent, newest first.
pub async fn list_workflows_for_agent(&self, agent_id: &Uuid) -> Result<Vec> {
let models = workflow_entity::Entity::find()
.filter(
Condition::all()
.add(workflow_entity::Column::AgentId.eq(agent_id.to_string()))
.add(workflow_entity::Column::Enabled.eq(1)),
)
.order_by(workflow_entity::Column::CreatedAt, Order::Desc)
.all(&self.db)
.await?;
models.into_iter().map(model_to_workflow).collect()
}
```
- Use it in `Scheduler::restart_workflows_for_agent` instead of `list_workflows(None)`.
Files
- `crates/orchestrator/src/scheduler/storage.rs` — add `list_workflows_for_agent`
- `crates/orchestrator/src/scheduler/mod.rs` — update `restart_workflows_for_agent` to use it
Context
Surfaced as a non-blocking suggestion in PR #1248 (code review item 4).
`Scheduler::restart_workflows_for_agent` calls `self.storage.list_workflows(None)` which loads all workflows from the database and filters by `agent_id` in Rust:
```rust
// scheduler/mod.rs
let workflows = self.storage.list_workflows(None).await?;
for workflow in workflows {
if workflow.agent_id != agent_id || !workflow.enabled {
continue;
}
...
}
```
`SchedulerStorage::list_workflows` currently only supports filtering by `project_id`, not `agent_id`. This means every agent restart performs a full table scan and discards unrelated rows in the application layer.
Why it matters
Restarts are rare so this is not an immediate problem, but as the workflow count grows this becomes a linear scan per restart. A targeted SQL query with a `WHERE agent_id = ?` clause would keep it constant regardless of total workflow count.
Proposed change
```rust
/// Lists all enabled workflows for a specific agent, newest first.
pub async fn list_workflows_for_agent(&self, agent_id: &Uuid) -> Result<Vec> {
let models = workflow_entity::Entity::find()
.filter(
Condition::all()
.add(workflow_entity::Column::AgentId.eq(agent_id.to_string()))
.add(workflow_entity::Column::Enabled.eq(1)),
)
.order_by(workflow_entity::Column::CreatedAt, Order::Desc)
.all(&self.db)
.await?;
models.into_iter().map(model_to_workflow).collect()
}
```
Files