Theory of non-Markovian optical gain in quantum-well lasers