Government agrees deal to protect social security rights of UK and Irish citizens living and working in each other’s state