David Petraeus, the former CIA director and top Army general whose affair with his biographer brought down what many considered a bright political future, has agreed to plead guilty to mishandling classified materials.