I was trying to use the method task.getCandidates() to get the list of candidate groups but it failed because the method is expecting to get a CommandContext.
If the command context is null, is it safe to manually initialise it? If yes, what command context should I instantiate?
Why a task is expecting to be in a CommandContext? Given that I can get an instance of Task using its taskId, I was expecting to get an object that is just an object representation of the task stored in the database.
Why does it depend on a context ?
yes, to have a command context initialized, execute your code inside a command. See org.flowable.engine.ManagementService#executeCommand(org.flowable.common.engine.impl.interceptor.Command<T>).
This is such important and usefull info it should be in the Docs. If you have any code common to your delegates and need that DelegateExecution this will solve your problems.