bugfix: don't write history longer than  window size.

Thanks to Bert Willaert for reporting.