Optimal coherent feedback for enhanced quantum technologies