During a 2016 experiment at Google in Mountain View, Calif., researchers gave a robot the task of lifting a pink stapler. The colorful items it had seen previously were soft baby clothes, so the machine figured that the stapler also “had to be soft,” said Sergey Levine, the scientist who led the work. But the pinching strategy it had used previously didn’t work in this case. The robot was foiled by the unexpected scenario.

In a factory, a robot’s movements are precisely choreographed. Engineers must reprogram the machine for...