*/ public function handle(int $workspaceId, ?string $status = null, ?string $planSlug = null, ?int $limit = null): Collection { if ($status !== null) { $valid = ['active', 'paused', 'completed', 'failed']; if (! in_array($status, $valid, true)) { throw new \InvalidArgumentException( sprintf('status must be one of: %s', implode(', ', $valid)) ); } } // Active sessions use the optimised service method if ($status === 'active' || $status === null) { return $this->sessionService->getActiveSessions($workspaceId); } $query = AgentSession::query() ->where('workspace_id', $workspaceId) ->where('status', $status) ->orderBy('last_active_at', 'desc'); if ($planSlug !== null) { $query->whereHas('plan', fn ($q) => $q->where('slug', $planSlug)); } if ($limit !== null && $limit > 0) { $query->limit(min($limit, 1000)); } return $query->get(); } }